Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SYSTEM_ALERT_WINDOW - How to get this permission automatically on Android 6.0 and targetSdkVersion 23

Facebook, Evernote, Pocket - all apps get this permission on Android 6.0 automatically, even though they are targeting 23 (targetSdkVersion=23).

There has been a lot of documentation regarding the new Marshmallow permission model. One of them is SYSTEM_ALERT_WINDOW been 'promoted' to 'above dangerous' permission class thus requiring a special user intervention in order for apps to be granted with those. If the app has targetSdkVersion 22 or lower, app gets this permission automatically (if requested in the manifest).

However, I've noticed some apps that get this permission, without needing to send the user to the setting special page of Draw over other apps permission. I saw Facebook, Evernote, Pocket - and perhaps there are more.

Anyone knows how an app can be granted this permission without the user go through Settings -> Apps -> Draw over other apps?

Thanks

like image 937
oriharel Avatar asked Mar 15 '16 16:03

oriharel


People also ask

What is android permission SYSTEM_ALERT_WINDOW?

The permission was introduced in Android 6.0 and it can allow an app to be displayed on top of another app, obscuring the lower-level app. In order to use the SYSTEM_ALERT_WINDOW permission, an app has to have explicit, manual approval from the user.

What is special permission in android?

Special Permissions These are those permissions that are neither Normal permissions nor Dangerous permissions. Most of the applications should not use these permissions because they are very sensitive and you need user authorization before using these permissions.

What is permission request code in android?

Requesting Android Runtime Permissions The method requestPermissions(String[] permissions, int requestCode); is a public method that is used to request dangerous permissions. We can ask for multiple dangerous permissions by passing a string array of permissions.

What is Android runtime permission?

Runtime permissions prevent apps from gaining access to private data without a user's consent, and provide them with additional context and visibility into the types of permissions that applications are either seeking, or have been granted.


2 Answers

It is a new behaviour introduced in Marshmallow 6.0.1.

Every app that requests the SYSTEM_ALERT_WINDOW permission and that is installed through the Play Store (version 6.0.5 or higher is required), will have granted the permission automatically.

If instead the app is sideloaded, the permission is not automatically granted. You can try to download and install the Evernote APK from apkmirror.com. As you can see you need to manually grant the permission in Settings -> Apps -> Draw over other apps.

These are the commits [1] [2] that allow the Play Store to give the automatic grant of the SYSTEM_ALERT_WINDOW permission.

like image 106
Mattia Maestrini Avatar answered Oct 05 '22 07:10

Mattia Maestrini


Yeh After Marshmallow come Android make security level more stick, But For

SYSTEM_ALERT_WINDOW 

you can show floating action and anything You can Force user to give permission for it By Following Codes in your onCreate() method Put this code after setContentView

    // Check if Android M or higher     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {         // Show alert dialog to the user saying a separate permission is needed         // Launch the settings activity if the user prefers         Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);         startActivity(myIntent);     } 

The action ACTION_MANAGE_OVERLAY_PERMISSION directly launches the 'Draw over other apps' permission screen.


Edit: My Above Code works 100% Correct

But I just found that many guys are still searching that how can allow ACTION_MANAGE_OVERLAY_PERMISSION permanently like If user has allow Permission Once then don't ask it every time he open application so hear solution for you-

  1. Check if device has API 23+

  2. if 23+ API then check if user has permit or not

  3. if had permit once don't drive him to Settings.ACTION_MANAGE_OVERLAY_PERMISSION and if has not permit yet then ask for runtime permission check

Put below line in your onCreate() method. Put this after setContentView

checkPermission(); 

Now put below code in onActivityResult

@TargetApi(Build.VERSION_CODES.M) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);      if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {         if (!Settings.canDrawOverlays(this)) {             // You don't have permission             checkPermission();         } else {             // Do as per your logic          }      }  } 

Now finally the checkPermission method code

public void checkPermission() {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {         if (!Settings.canDrawOverlays(this)) {             Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,                     Uri.parse("package:" + getPackageName()));             startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);         }     } } 

And don't forget to declare this public variable in your class

public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 5469; 
like image 44
Jayman Jani Avatar answered Oct 05 '22 08:10

Jayman Jani