我想在创建活动时加载一些联系人。因为这是一个长时间运行的操作,我想通过ProgressDialog通知用户。对这个应用程序的请求是不使用AsyncTasks所以我使用线程,但进度没有显示。这是我的onCreate方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
handler = new Handler();
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
progress = new ProgressDialog(this);
progress.setMessage("Loading Contacts ");
progress.setIndeterminate(true);
progress.show();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
contacts = ContactsUtils.getContacts(getContentResolver());
handler.post(new Runnable() {
public void run() {
progress.dismiss();
// UpdateDisplay();
};
});
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
Log.e("Interrupted Exception:", e.getLocalizedMessage());
}
// other methods
}
我错过了什么吗?感谢
已解决:在加入之后,onCreate的其余代码应移至处理程序,并应删除加入
答案 0 :(得分:1)
你是missig
progress.show()
加上为什么你使用线程你不认为异步任务会以美丽的方式完成这项任务吗?
您没有看到进度对话框的问题是因为
try {
t.join();
} catch (InterruptedException e) {
Log.e("Interrupted Exception:", e.getLocalizedMessage());
}
这保持主线程直到你的任务完成,只需评论
答案 1 :(得分:0)
Try This Code:-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launching);
final ProgressDialog myPd_ring=ProgressDialog.show(Launching.this, "", "Loading please wait..", true);
myPd_ring.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try
{
Thread.sleep(2000);
}catch(Exception e){}
Intent intt=new Intent();
intt.setClass(Launching.this, MainController.class);
startActivity(intt);
finish();
myPd_ring.dismiss();
}
}).start();
}
答案 2 :(得分:0)
每当您尝试使用后台线程中的UI执行某些操作时,请使用Handler
或在runOnUiThread(Runnable())
答案 3 :(得分:0)
尝试这样的事情:
private ProgressDialog pDialog; // global
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
color_list = (ListView) this.findViewById(R.id.color_list);
imageView1 = (ImageView)findViewById(R.id.icon);
pDialog = new ProgressDialog(Launcher.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
showProgressDialog();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
contacts = ContactsUtils.getContacts(getContentResolver());
handler.post(new Runnable() {
public void run() {
hideProgressDialog();
};
});
}
});
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}