-1

I am working on run time permissions. When I am in my activity class while creating a report having images, when permission is changed app crashed and I lost my data.I have used OnsaveInstance but it does not do what I really want. I want to restart my app when someone change app permission from settings and save my report.

  • which method of the activity is called when permission changed?.

  • How i know that permission is changed from background?

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
Basit Ali
  • 81
  • 10

3 Answers3

1

You can check permission in your activity startup or resume, like this answer

and for restarting app you can try any answer from this Q/ans thread, one of the accepted answer for restarting activity here

Intent i = getBaseContext().getPackageManager()
             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Community
  • 1
  • 1
Fahadsk
  • 1,099
  • 10
  • 24
1

How i know that permission is changed from background.?

If, from Settings, the user removes a permission that the user granted previously, your process is terminated immediately. You find out that the permission changed the next time your app runs.

If, from Settings, the user grants a permission that the user had not granted previously, then your process is left alone, but you also are not informed about the new permission. You are welcome to call checkSelfPermission() at appropriate points to see if you now have the permission that you lacked before.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

To check if you currently have a permission you use ContextCompat.checkSelfPermission(), see the developer docs for details.

If you need the permission in a background thread then you check if you have the permission before you do the work. If you do not have the permission you will have to skip the work, if you just continue then your code will most likely throw a SecurityException.

How you handle it when you don't have the required permissions depends on the use case. You could create a system notification that informs the user the background task could not be performed due to a missing permission. If the user taps the notification then you can request the permission from the user before retrying the task. See the same developer docs for how to request a permission from the user.

If it would be okay to abort the background task then you could do just that but you will probably want to show somewhere in your user interface that the background task needs a permission that is missing and ask the user to grant it so the (optional) background task can do its work.

Rob Meeuwisse
  • 2,847
  • 1
  • 17
  • 21