progressDialog in onNewIntent



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

您不能在另一个线程上更新或使用UI:

解决方案:

调用主线程并更新内部的UI

    Thread scanning = new Thread(new Runnable() {
        public void run() {
            runOnUiThread(new Runnable() 
            {
               public void run() 
               {
                    ScanDialog = ProgressDialog.show(BorrowActivity.this,
                        "Scanning...", "scanning");
               }
            });
        }
 });

最后我用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) {
    }
}

相关内容

  • 没有找到相关文章