[英]progressDialog in onNewIntent
I am using onNewIntent when I am scanning NFC tags. 扫描NFC标签时,我正在使用onNewIntent。 I want to show ProgressDialog while tag is scanned. 我想在扫描标签时显示ProgressDialog。 I tried use a thread but it crashed my app. 我尝试使用线程,但崩溃了我的应用程序。 Is there some way how I can show progressDialog when onNewIntent starts? 当onNewIntent启动时,有什么方法可以显示progressDialog吗?
public void onNewIntent(Intent intent) {
setIntent(intent);
Thread scanning = new Thread(new Runnable() {
public void run() {
ScanDialog = ProgressDialog.show(BorrowActivity.this,
"Scanning...", "scanning");
}
});
scanning.start();
.
. //next code doing something
.
}
You cannot update or use a UI on another thread: 您不能在另一个线程上更新或使用UI:
solution: 解:
Call the Main thread and update the UI inside there 调用主线程并在那里更新用户界面
Thread scanning = new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable()
{
public void run()
{
ScanDialog = ProgressDialog.show(BorrowActivity.this,
"Scanning...", "scanning");
}
});
}
});
Finally I fixed it with asyncTask. 最后,我用asyncTask修复了它。
public void onNewIntent(Intent intent) {
setIntent(intent);
ScanDialog = ProgressDialog.show(BorrowActivity.this,
"Scanning...", "Scanning");
try {
new DoBackgroundTask().execute();
} catch (Exception e) {
//error catch here
}
ScanDialog.dismiss();
And AsyncTask: 和AsyncTask:
private class DoBackgroundTask extends AsyncTask<Integer, String, Integer> {
protected Integer doInBackground(Integer... status) {
//do something
}
protected void onProgressUpdate(String... message) {
}
protected void onPostExecute(Integer status) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.