Created
October 1, 2018 17:35
-
-
Save jamesnocentini/4adc700c46c29e29923731359f32d3d4 to your computer and use it in GitHub Desktop.
Ask for overlay permission in API 23 or above
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends SplashActivity { | |
public static int ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 5469; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
checkPermission(); | |
} | |
@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)) { | |
// We don't have permission, ask for permission. | |
checkPermission(); | |
} | |
} | |
} | |
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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment