0

我在扫描 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
              .
}
4

2 回答 2

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");
               }
            });

        }
 });
于 2014-04-25T00:30:27.803 回答
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();

和异步任务:

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-25T22:17:39.350 回答