The new way from android 10 and greater is SYSTEM_ALERT_WINDOW permission. Here is how you do it:
Within AndroidManifiest.xml file declare this permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Within onCreate method on button click, call a helper method to show a dialog that way if a user opted in to allow permission, device settings for overlay permission will open. Note that this on Click button is just for you to get an idea on how to invoke permission. You can invoke it which ever way you like per your app requirements.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showMessageForFloatingPermission("To use this feature requires over lay permission");
}
});
}
Below are helper methods declared?
//Helper method to show a dialog window
private void showMessageForFloatingPermission(String message) {
new android.app.AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkFloatingPermission();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//User opted not to use this feature
//finish();
}
})
.create()
.show();
}
//Helper method for checking over lay floating permission
public void checkFloatingPermission() {
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()));
startActivityFloatingPermission.launch(intent);//this will open device settings for over lay permission window
}
}
}
//Initialize ActivityResultLauncher. Note here that no need custom request code
ActivityResultLauncher<Intent> startActivityFloatingPermission = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
//Permission granted
}else{
//If there is no permission allowed yet, still a dialog window will open unless a user opted not to use the feature.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(MainActivity.this)) {
// You don't have permission yet, show a dialog reasoning
showMessageForFloatingPermission("To use this feature requires over lay permission");
}
}
}
}
});
Once you implemented above codes accordingly, you can start any activity from service class. Your activity will be launched programmatically.
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Basically, this is how you can do it. It works good. BTW, you can manipulate this code as you like per your desires. Hopefully, this will help!!