Android runtime permissions- how to implement

What is “MY_PERMISSIONS_REQUEST_READ_CONTACTS” in this example?

It is an int, to tie a particular requestPermissions() call to the corresponding onRequestPermissionsResult() callback.

Under the covers, requestPermissions() uses startActivityForResult(); this int serves the same role as it does in startActivityForResult().

does that mean I should make a Constants.java and declare a public static int?

I would just make it a private static final int in the activity. But, you can declare it wherever you want.

What should the value be?

I seem to recall that it needs to be below 0x8000000, but otherwise it can be whatever you want. The value that you use for each requestPermissions() call in an activity should get a distinct int, but the actual numbers do not matter.

If your activity has only one requestPermissions() call, then the int value really does not matter. But many apps will have several requestPermissions() calls in an activity. In that case, the developer may need to know, in onRequestPermissionsResult(), what request this is the result for.

Leave a Comment