12

Is there a way to get callbacks if the user revokes permissions from the settings after having granted them?

I know we have to check for permissions before trying to use a camera etc etc.

Consider the following scenario, I have a video processing application which needs permission to read and write to storage. The task of processing is usually in the order of seconds. Assuming the user gave permissions when asked and revokes them after the processing starts. In this case is there a way we can get callbacks rather than we checking each time? Just so that we can handle the error case gracefully.

LostPuppy
  • 2,471
  • 4
  • 23
  • 34
  • Can't you just catch the exception? – Pablo Oct 05 '15 at 21:49
  • I could do that. Was wondering if there was a better way of doing it. – LostPuppy Oct 05 '15 at 22:09
  • 1
    Why is it not a good way? Exceptions are for that: exceptional situations. – Pablo Oct 06 '15 at 00:32
  • The assumption here is the user can take permission off anytime and I don't like the idea of wrapping my entire video processing logic in a blanket try catch. And as the commonsware account pointed out, It sounds more like something wrong with how I am handling the processing logic. – LostPuppy Oct 06 '15 at 20:10
  • I understand. I don't think that is relevant to what I was trying to say, but I must say I was wrong when I assumed the code would launch an exception. I just found out it only throws an exception if the permission was never given. When it is revoked, the code will simply do nothing. Honestly, I don't like this behaviour, but what do I know... – Pablo Oct 07 '15 at 09:15
  • That's not true, depending on what permission you revoked it does give you an exception. In my case when I revoke storage permission it gives a EACCES (Permission denied) exception. – LostPuppy Oct 07 '15 at 17:52
  • I'll have to look at that better. I said that because I read it in a blog post, but it was not official and was written when 6.0 was announced, so it's possible things have changed (or the author didn't know what he was talking about). – Pablo Oct 07 '15 at 19:51
  • Try this it may be work http://stackoverflow.com/a/41221852/5488468 – Bipin Bharti Jan 03 '17 at 11:08

1 Answers1

7

Is there a way to get callbacks if the user revokes permissions from the settings after having granted them?

No, because your process gets terminated. There is nothing to call back to.

Assuming the user gave permissions when asked and revokes them after the processing starts

In that case, your processing ends abruptly, when your process is terminated.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Currently, when the process ends abruptly the app does not handle it in a gracious way. So was wondering if there was a better way of handling it rather than just having try catch all over the place. – LostPuppy Oct 05 '15 at 22:44
  • 2
    @LostPuppy: There is no "try catch all over the place" for stopping the process termination. Your process is simply terminated. It's in the background (since the user is in Settings), and your app can be terminated when it's in the background at any point, by the OS or the user (e.g., task killers). That has nothing to do with runtime permissions, other than it being yet another trigger for this behavior. So, if your "app does not handle [process termination] in a gracious way", that's something that you need to fix. – CommonsWare Oct 05 '15 at 22:53
  • Is it documented anywhere that revoking a granted permission kills the process? I know it happening but i can't find it in the documentation anywhere? – Joe Maher Apr 17 '16 at 02:59
  • @JoeMaher: Considering that the documentation sucks for runtime permissions, this does not surprise me. :-( – CommonsWare Apr 17 '16 at 11:10
  • @CommonsWare Current activity is recreated but crash on previous activity. How do i handle? – Hiren Dabhi Jun 28 '16 at 11:41
  • @HirenDabhi: I do not understand your question, sorry. – CommonsWare Jun 28 '16 at 11:45
  • @CommonsWare Steps to reproduce: 1) ActivityA (2 tabs with fragment )->startActivityForResult(AcitivityB). 2) in ActivityB, before capturing image/gallery on button clicked, i revoked the permissions. 3) ActivityB: request again to capture image/gallery. on successful i was passing the return to ActivityA on onActivityResult() and try to reload fragments. When i debug my app, what i observed, fragment views are null. so i could not refresh fragment(photos). – Hiren Dabhi Jun 28 '16 at 12:04
  • 1
    @HirenDabhi: Your entire process was terminated. This is not unique to permission revocation. You will be better served asking a fresh Stack Overflow question, with a [mcve] demonstrating your problem (code, stack trace, etc.). – CommonsWare Jun 28 '16 at 12:19
  • I am downloading some file and showing notification with downloading progress, Now I am revoking the storage permission. App restarts but the notification remains forever even unable to delete it manually.How to clear these notifications? – pradeep_ch Sep 19 '16 at 14:45
  • @pradeep_ch: I would expect that `cancel()` or `cancelAll()` on `NotificationManager` would work. If you are running into problems with that, ask a separate Stack Overflow question, where you provide a [mcve] demonstrating your problem. – CommonsWare Sep 19 '16 at 14:47
  • @CommonsWare What do u have to say about SYSTEM_ALERT_WINDOW / "Draw over other apps" permission being revoked. I dont think the process gets terminated if we revoke this permission. – Ali Ashraf Nov 30 '16 at 11:01
  • @Ali: I can confirm that your process is not terminated when `SYSTEM_ALERT_WINDOW` is revoked. IMHO, this is a bug, but the whole `SYSTEM_ALERT_WINDOW` stuff is a mess at the moment. Since I avoid this permission in general, I do not have any specific recommendations for apps that use it. – CommonsWare Nov 30 '16 at 15:20
  • Is it documented anywhere that the process is terminated when a permission is revoked? – Kevin Krumwiede Mar 16 '17 at 05:05
  • @KevinKrumwiede: It had been, at one point, as part of the M Developer Preview and Android 6.0 rollout. I do not see where the current documentation states it, though. – CommonsWare Mar 16 '17 at 11:48