我在扫描NFC标签时正在使用onNewIntent。我想在扫描标签时显示ProgressDialog。我尝试使用一个线程,但它崩溃了我的应用程序。在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
.
}
答案 0 :(得分:0)
您无法在其他线程上更新或使用UI:
<强>溶液强>
调用主线程并更新其中的UI
Thread scanning = new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable()
{
public void run()
{
ScanDialog = ProgressDialog.show(BorrowActivity.this,
"Scanning...", "scanning");
}
});
}
});
答案 1 :(得分:0)
最后我用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();
和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) {
}
}