Chapter 33 Locations and Play Services
Challenge: Permissions Rationale
As mentioned above, the system permission dialog is not descriptive enough for many applications. In
those cases, you need to provide the user with a rationale.
Android has a preferred flow for displaying this rationale. As of this writing, it works as follows:
- The first time the user asks, show the system dialog.
- Any time the user asks after that, show a dialog with your explanation, then the system dialog
again. - If the user requests to deny the permission forever, never show the rationale or the system dialog
again.
This may seem a little complicated. Luckily, Android provides a method to help you
implement it: ActivityCompat’s shouldShowRequestPermissionRationale(...).
shouldShowRequestPermissionRationale(...) will return false before you first request a permission,
true after it has initially been denied, and then false if the user chooses to permanently deny the
permission.
For this challenge, implement a rationale DialogFragment that displays a short message: “Locatr uses
location data to find images near you on Flickr.” Use shouldShowRequestPermissionRationale(...) to
see whether the rationale should be displayed before calling requestPermission(...). If the rationale
is displayed, Locatr should wait until it is dismissed by the user to call requestPermission(...). (Hint:
You can detect dismissal by overriding DialogFragment.onCancel(...).)
Challenge: Progress
This simple app could use some more feedback in its interface. There is no immediate indication when
you press the button that anything has happened.
For this challenge, modify Locatr so that it responds immediately to a press by displaying a progress
indicator. The ProgressDialog class can show a spinning progress indicator that will do the trick
nicely. You will also need to track when SearchTask is running so that you can clear away the progress
when that is appropriate.