错误从Service启动AlertDialog.Builder

时间:2017-09-17 13:22:44

标签: android

大家好我试图在推送通知收到时从服务启动CountDownTimer对话框。我想要唤醒屏幕并使用系统警报对话框;  我使用权限:

  

“android.permission.SYSTEM_ALERT_WINDOW”,“android.permission.WAKE_LOCK”

但是当收到通知时,请获取以下错误和服务carash:

                                                                    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
                                                                       at android.os.Handler.<init>(Handler.java:200)

                                                                       at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:46)
                                                                       at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:97)
                                                                       at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:929)
                                                                       at com.iransnaptaxi.taxi.Server.PusheServic.ShowDialog(PusheServic.java:91)
                                                                       at com.iransnaptaxi.taxi.Server.PusheServic.onMessageReceived(PusheServic.java:48)

                                                                       at android.os.AsyncTask$2.call(AsyncTask.java:297)
                                                                       at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 

我尝试用下面的鳕鱼来填充它:

private void ShowDialog(){

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK| PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "taxiWakeLock");
    wakeLock.acquire();



    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getApplicationContext(), R.style.AppTheme_MaterialDialogTheme);

     dialogBuilder.setTitle("درخواست سفر")
            .setMessage("درخواست سفر دارید").setNegativeButton("لغو", null)
            .setPositiveButton("مشاهد سفر", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {



                }
            });

    final AlertDialog dialog = dialogBuilder.create();
    final Window dialogWindow = dialog.getWindow();
    final WindowManager.LayoutParams dialogWindowAttributes = dialogWindow.getAttributes();


    dialogWindow.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    dialogWindowAttributes.windowAnimations = R.anim.dialogue_scale_anim_open;
    dialog.show();
    new CountDownTimer(15000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            dialog.setMessage("زمان مانده تا لغو درخواست"+"00:"+ (millisUntilFinished/1000));
        }

        @Override
        public void onFinish() {
            dialog.dismiss();
            //info.setVisibility(View.GONE);
        }
    }.start();



}

1 个答案:

答案 0 :(得分:0)

好像您正试图通过doInBackground()的{​​{1}}方法显示对话框。从AsyncTask方法显示它。 UI元素只能在UI线程中使用,而不能从后台线程使用。

如果您收到Looper错误,请尝试此操作:

onPostExecute()

您还需要Looper.prepare(); showDialog(); Looper.loop(); 上下文来创建AlertDialogs。请参阅答案here以从服务内部创建对话框。