簡體   English   中英

onNewIntent中的progressDialog

[英]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:

解:

調用主線程並在那里更新用戶界面

    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();

和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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM