1

hello everybody I'm trying to launch CountDownTimer dialog from Service when push notification receive . i want wake up screen and use system alert dialog ; I use permissions :

"android.permission.SYSTEM_ALERT_WINDOW", "android.permission.WAKE_LOCK"

but when get notification, get below error and service 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) 

and I tried to luanch it with below cod:

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



}
amin mahmoudi
  • 630
  • 7
  • 26

1 Answers1

1

Seems like you are trying to show dialog from doInBackground() method of AsyncTask. Show it from the onPostExecute() method. UI elements can only be used from the UI thread not from the background thread.

Try this if you get Looper error:

Looper.prepare();
showDialog();
Looper.loop();

Also you need Activity context to create AlertDialogs. See the answer here to create dialogs from inside the service.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • i call this method from class that extend service created by pushe library and u can see methods of class [here](https://imgur.com/kiqoRQW) – amin mahmoudi Sep 17 '17 at 13:47
  • Your stacktrace indicates that the method is being called from an AsyncTask. Try calling adding `Looper.prepare();` before showDialog(); and `Looper.loop();` after it. – Nabin Bhandari Sep 17 '17 at 13:52
  • thank you very much ,'Looper' work for me pleas edit your main answer to help other have same problem , – amin mahmoudi Sep 17 '17 at 14:14
  • Hello again , This works great on Android 5, but does not work on Android 6 and above, although the TYPE_SYSTEM_OVERLAY access permission is enabled in the settings. can you help me ? – amin mahmoudi Sep 18 '17 at 17:11
  • check this question and it's answers. https://stackoverflow.com/questions/40337131/how-to-enable-screen-overlay-permission-by-default – Nabin Bhandari Sep 19 '17 at 01:16