How to Give Screen Overlay Permission On My Activity

后端 未结 3 1936
眼角桃花
眼角桃花 2020-12-15 06:26

In My application I am getting Screen Overlay Issue in android 6+ I tried to Turn on But for that I need to Give a Permission for Screen Overlay

I followed this I am

3条回答
  •  温柔的废话
    2020-12-15 06:32

    The second post you have checked clearly shows the way of checking for SYSTEM_ALERT_WINDOW permission. But to simply and explain,

    As mentioned on developer.android.com

    Allows an app to create windows using the type TYPE_SYSTEM_ALERT, shown on top of all other apps. Very few apps should use this permission; these windows are intended for system-level interaction with the user.

    Note: If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action ACTION_MANAGE_OVERLAY_PERMISSION. The app can check whether it has this authorization by calling Settings.canDrawOverlays().

    and as mentioned in the SO post you checked,

    Here are simplified steps:-

    1. First check whether current device SDK version is greater than or equal to Android M (23) by following if condition

      if (android.os.Build.VERSION.SDK_INT >= 23) {
      }
      
    2. Then using Settings.canDrawOverlays() as mentioned in the developer.android.com check whether your application already have permission or not, we will check for do not have permission

      if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
      }
      
    3. Then as mentioned in developer.android.com and as implemented in the SO post trigger an intent with ACTION_MANAGE_OVERLAY_PERMISSION.

      if (android.os.Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {   //Android M Or Over
         Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
         startActivityForResult(intent, );
         return;
      }
      
    4. Handle the result in onActivityResult() method defined in Activity, and again check using Settings.canDrawOverlays() if still not then finish() the activity after showing appropriate alert to user.

    This whole flow you can implement after other permission flow is completed.

提交回复
热议问题