Frage

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
              .
}
War es hilfreich?

Lösung 2

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

Andere Tipps

You cannot update or use a UI on another thread:

solution:

Call the Main thread and update the UI inside there

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

        }
 });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top