Skip to content Skip to sidebar Skip to footer

Permission Error Is Still Showing Even When Added Permission In Manifest

I implemented AlertDialog in my class which extend Application class, also added proper permission in manifest file: Copy

In onActivityResult,

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
        if (!Settings.canDrawOverlays(this)) {
            // SYSTEM_ALERT_WINDOW permission not granted...
        }
    }
}

Finally----

Check if the device has API 23+

if 23+ API then check if the user has permission or not

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

Put below the 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 permissioncheckPermission();
        } else {
            // Do as per your logic 
        }
    }
}

Now finally the checkPermission method code,

publicvoidcheckPermission() {
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 declare a variable as global

publicstaticint ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 5469;

Happy Coding :)

Post a Comment for "Permission Error Is Still Showing Even When Added Permission In Manifest"