0

I am trying to create an activity that would pop up in full screen and be shown over the screen lock (similar to every alarm clock in Android which shows a full screen dialog to dismiss an alarm). Here is my code to display the window:

    if (Build.VERSION.SDK_INT>=26) windowType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    else windowType = WindowManager.LayoutParams.TYPE_TOAST;
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            windowType,
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT
    );
    wm = (WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);

    mTopView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_alarm_screen, null);
    getWindow().setAttributes(params);
    wm.addView(mTopView, params);

My concern here is that my app will need a special permission to show this window over other applications, and this scares users off. In API25 and lower I used TYPE_TOAST instead of TYPE_APPLICATION_OVERLAY, but in API26 it leads to:

    FATAL EXCEPTION: main Process: com.app.alarm, PID: 31372
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.alarm/com.app.alarm.Activities.AlarmScreenActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@601b8a2 -- permission denied for window type 2009 
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)

I saw this question, but it does not avoid asking a special permission.

Any other trick in API26 to avoid making a user allow a special permission that my app doesn't really need?

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33

0 Answers0