来自线程的 Android 对话框

发布于 2024-12-02 07:00:43 字数 2556 浏览 0 评论 0原文

在以 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);是什么导致了崩溃?如果是这样,为什么它不会持续崩溃。如果不是,知道是什么吗?非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2024-12-09 07:00:43

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文