问题描述
我要显示一个进度对话框装载,而我的应用程序打开。
我试过的AsyncTask和其他一些方法(Uithread,线程)来做到这一点,所有的都失败了。
我不知道为什么,但progressdialog是不是在我的手机屏幕上显示。我总是看到一个黑暗的画面。
getApnData抛出异常后,我的活动UI组件显示,进度对话框的作品,但为时已晚
下面是我的一些code部分。
// 1-1-1-1-1
//在创建或的OnStartLoginProgressTask任务1 =新LoginProgressTask();
task1.execute();//内部类
类LoginProgressTask扩展的AsyncTask<字符串,整数,布尔> {
@覆盖
保护布尔doInBackground(字符串... PARAMS){
尝试{
getApnData(); //在这里做你的实际工作
}赶上(例外五){
e.printStackTrace();
}
返回Boolean.TRUE; //这里返回自己的真实结果
} @覆盖
在preExecute保护无效(){
super.on preExecute();
LoginUI.this.pd = ProgressDialog.show(LoginUI.this,,载入中...,真实,真实);
} @覆盖
保护无效onPostExecute(布尔结果){
// LoginUI.this.pd.dismiss(); }
}
// 2-2-2-2-2
// OnCreate中或的OnStart
PD = ProgressDialog.show(这一点,,载入中...,真,假);线程=新主题(本);
thread.start();
//跑
@覆盖
公共无效的run(){
尝试{
firstRunUI();
handler.sendEmptyMessage(0);
}赶上(例外五){
e.printStackTrace();
} }// UIRun
公共无效firstRunUI(){
runOnUiThread(新的Runnable(){ @覆盖
公共无效的run(){
尝试{
getApnData();
}赶上(例外五){
e.printStackTrace();
} }
});
}//处理程序
私人处理程序处理程序=新的处理程序(){
公共无效的handleMessage(消息MSG){
pd.dismiss();
}
};
好吧首先,
您可以使用下面的场景显示进度:
-
如下使用您LoginProgressTask
新LoginProgressTask()执行();
-
修改
LoginUI.this.pd = ProgressDialog.show(LoginUI.this,,载入中...,真实,真实)
-
使用的问题上,请使用以下的onCreate或在onStart代替code的你的第二块
新LoginProgressTask()执行();
下面是一个完整的例子
类SomeActivity延伸活动{
ProgressDialog PD;
类LoginProgressTask扩展的AsyncTask<字符串,整数,布尔> {
@覆盖
在preExecute公共无效(){
PD = ProgressDialog.show(LoginUI.this,,载入中...,真实,真实);
}
@覆盖
保护布尔doInBackground(字符串... PARAMS){
尝试{
getApnData(); //在这里做你的实际工作
}赶上(例外五){
e.printStackTrace();
}
返回Boolean.TRUE; //这里返回自己的真实结果
}
} @覆盖
保护无效onPostExecute(布尔结果){
pd.dismiss(); //或使用处理程序最好是 }
} @覆盖
保护无效的onCreate(捆绑savedInstanceState){
新LoginProgressTask()执行();
}
}
这将做的工作适合你!
河畔确保你解雇ProgressDialog上PostExecute方法*
注意
不要创建主UI线程意见,因为这可能会导致内存泄漏
更新
从getAbnData要更新UI元素()
您应该使用处理程序
处理程序处理程序=新Hanlder(){
@覆盖
公共无效的handleMessage(INT什么){
//改变UI元素code来这里}
}
从code使用内handler.sendMessage(NUMER)
,然后将处理信息
通讯
I want to show a progress dialog "loading" while my app is opening.
I tried AsyncTask and some other methodologies(Uithread, thread) to do this and all of were failed.
I do not know why, but the progressdialog is not showed in my phone screen. I always see a dark screen.
After getApnData throws exception and my activity UI components are shown, progress dialog works, but it's too late
Here is some of my code parts.
//1-1-1-1-1
//On create or OnStart
LoginProgressTask task1 = new LoginProgressTask();
task1.execute();
//Inner class
class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
getApnData(); // Do your real work here
} catch (Exception e) {
e.printStackTrace();
}
return Boolean.TRUE; // Return your real result here
}
@Override
protected void onPreExecute() {
super.onPreExecute();
LoginUI.this.pd = ProgressDialog.show(LoginUI.this, "", "Loading...", true, true);
}
@Override
protected void onPostExecute(Boolean result) {
// LoginUI.this.pd.dismiss();
}
}
//2-2-2-2-2
//OnCreate or OnStart
pd = ProgressDialog.show(this, "", "Loading...", true, false);
thread = new Thread(this);
thread.start();
//run
@Override
public void run() {
try {
firstRunUI();
handler.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
}
//UIRun
public void firstRunUI() {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
getApnData();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//Handler
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pd.dismiss();
}
};
Ok first of all ,You can show progress using the following scenario:
Use your LoginProgressTask as follows
new LoginProgressTask().execute();
Change
LoginUI.this.pd = ProgressDialog.show(LoginUI.this, "", "Loading...", true, true)
Use the following on use onCreate or onStart instead of your 2nd block of code in the question
new LoginProgressTask().execute();
Here are a complete example
class SomeActivity extends Activity {
ProgressDialog pd ;
class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
@Override
public void onPreExecute(){
pd = ProgressDialog.show(LoginUI.this, "", "Loading...", true, true);
}
@Override
protected Boolean doInBackground(String... params) {
try {
getApnData(); // Do your real work here
} catch (Exception e) {
e.printStackTrace();
}
return Boolean.TRUE; // Return your real result here
}
}
@Override
protected void onPostExecute(Boolean result) {
pd.dismiss(); //or use handlers it is better
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
new LoginProgressTask().execute();
}
}
This will do the job for you!!
Nad make sure you dismiss the ProgressDialog on PostExecute method*
NOTEDon't Create Views in Main UI thread because that may cause memory leak
UpdateTo update UI Elements from getAbnData()You should use Handlers
Handler handler = new Hanlder(){
@Override
public void handleMessage(int what){
//changing UI Elements code comes here
}
}
from within the code use handler.sendMessage(NUMER)
and then it will handle message
Communicating with the UI Thread
这篇关于在应用程序启动Progressdialog不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!