Search code examples
androidmultithreadingprogressdialogonnewintent

progressDialog in onNewIntent


I am using onNewIntent when I am scanning NFC tags. I want to show ProgressDialog while tag is scanned. I tried use a thread but it crashed my app. Is there some way how I can show progressDialog when onNewIntent starts?

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

Solution

  • Finally I fixed it with 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();
    

    And 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) {
        }
    }