I need to access location from several non-UI components in my app. Since the app may not have the required permission, I implemented a solution outlined in the answers to How to request permissions from a service in Android Marshmallow: if the app doesn't have permission, it displays a notification. Tapping that notification will take the user to an empty Activity
, which displays the runtime permissions dialog on top.
However, I have the following issue now: If the user dismisses that dialog without making a choice (e.g. by tapping Home) and later brings up any other activity of my app, the runtime permission dialog reappears instead of the desired activity.
Here's my code for the notification:
Intent permIntent = new Intent(context, PermissionRequestActivity.class);
permIntent.putExtra(Const.KEY_RESULT_RECEIVER, resultReceiver);
permIntent.putExtra(Const.KEY_PERMISSIONS, permissions);
permIntent.putExtra(Const.KEY_REQUEST_CODE, requestCode);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PermissionRequestActivity.class);
stackBuilder.addNextIntent(permIntent);
PendingIntent permPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(notificationIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setOngoing(true)
.setAutoCancel(true)
.setWhen(0)
.setContentIntent(permPendingIntent)
.setStyle(null);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, builder.build());
Is there a way I can abort the runtime permissions dialog when its Activity
is stopped?