yeh After the release of Marshmallow, Android will make the security level more stick, but for
SYSTEM_ALERT_WINDOW
you can show a floating action and all that you can get the user to give permission to it. Following the 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_MANAGE_OVERLAY_PERMISSION action directly launches the "Draw on other applications" permission screen.
Edit
Hi my above code is working 100% Correct
but I just found that many guys are still looking for how to enable ACTION_MANAGE_OVERLAY_PERMISSION for a long time, as if the user has allowed permission. After that, do not ask him every time he opens the application , so listen to the solution for you- 1) Check that the device has api 23+
2) if 23+ api then check if user is allowed or not
3) if he allowed once not to deliver it to Settings.ACTION_MANAGE_OVERLAY_PERMISSION, and if he hasn’t yet, ask him to check the permission at run time
Put below line in 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)) {
Now, finally, the code of the checkPermission method
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;
greetings !; -)