来自线程的Android对话框

时间:2011-08-31 23:06:25

标签: android android-asynctask

在使用AsyncTask启动的后台线程中,我请求root访问权限。如果它被拒绝(su未找到),我想告诉用户。我知道你不能直接从后台线程发布到UI线程,但是当我测试它时,通过引用主活动这样做似乎运行良好。现在它已经发布到市场上,但是报告了这些错误:

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

这是对话框创建者中的代码(第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;

后台线程中的第501-511行,我将对话框调用失败。

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

是activity.showDialog(1);是什么导致了崩溃?如果是这样,为什么它不会一直崩溃。如果不是,任何想法是什么?非常感谢。

1 个答案:

答案 0 :(得分:1)

您的代码仍尝试从后台线程显示对话框。

在您的特定情况下,您要做的是从doInBackground()返回结果,如果结果表明找不到su,请在onPostExecute()中显示对话框。< / p>