jarvisteve
jarvisteve

Reputation: 926

Android dialog from thread

In a background thread started with AsyncTask I request root access. If it's denied (su not found), I want to tell the user. I know you can't directly post to the UI thread from a background thread, but doing so through a reference to the main activity seemed to work well when I tested it. Now that it's published to the market, though, these errors are reported:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:266)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not     called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.app.Dialog.<init>(Dialog.java:100)
at android.app.AlertDialog.<init>(AlertDialog.java:96)
at android.app.AlertDialog$Builder.create(AlertDialog.java:891)
at com.sajarvis.albert.AlbertActivity.onCreateDialog(AlbertActivity.java:357)
at android.app.Activity.onCreateDialog(Activity.java:2747)
at android.app.Activity.createDialog(Activity.java:948)
at android.app.Activity.showDialog(Activity.java:2825)
at android.app.Activity.showDialog(Activity.java:2789)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:507)
at com.sajarvis.albert.AlbertActivity$HiyaTask.doInBackground(AlbertActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more

Here's the code in the dialog creator (lines 349-360)

case 1: //su denied
    Log.w(TAG,"Su denied failure");
    builder
        .setTitle(R.string.su_denied_title)
        .setMessage(R.string.su_denied_message)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
    }).create();
break;

And lines 501-511 in the background thread, where I call the dialog on fail.

//Get ROOT access.
try {
    process = Runtime.getRuntime().exec("su");
} catch (IOException e1) {
    //TODO crashes here
    activity.showDialog(1);
    FlurryAgent.onError("4","su denied","Fail");

    return success; //False, fail.
}

Is activity.showDialog(1); what's causing the crash? If so, why does it not crash consistently. If it isn't, any idea what is? Thanks much.

Upvotes: 0

Views: 709

Answers (1)

Alex Gitelman
Alex Gitelman

Reputation: 24732

Your code still attempts to show dialog from background thread.

In you particular case, what you want to do is to return a result from doInBackground() and if result indicates that su can't be found, show dialog in onPostExecute().

Upvotes: 1

Related Questions