当我扫描NFC标签时,我正在使用onNewIntent。我想在扫描标签时显示ProgressDialog。我试着用一个线程,但它毁了我的应用程序。有什么方法可以在progressDialog启动时向onNewIntent展示?
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
.
}发布于 2014-04-25 22:17:39
最后,我用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) {
}
}发布于 2014-04-25 00:30:27
不能在另一个线程上更新或使用UI:
解决方案:
调用主线程并更新其中的UI
Thread scanning = new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable()
{
public void run()
{
ScanDialog = ProgressDialog.show(BorrowActivity.this,
"Scanning...", "scanning");
}
});
}
});https://stackoverflow.com/questions/23282120
复制相似问题