Chapter 33 Locations and Play Services
Checking for permissions
Your first step will be to pull your permissions information into your Java code. You can do this by
adding a constant array to the top of LocatrFragment that lists all the permissions you need.
Listing 33.17 Adding permissions constants (LocatrFragment.java)
public class LocatrFragment extends Fragment {
private static final String TAG = "LocatrFragment";
private static final String[] LOCATION_PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
};
private ImageView mImageView;
private GoogleApiClient mClient;
All standard Android permissions are declared in the Manifest.permission class for you to
use programmatically. So these two constants refer to the same string values you used in your
AndroidManifest.xml. With your permissions array declared, your next step is to ask for the
permissions you need.
Permission is granted for dangerous permissions on the basis of permission groups, not individual
permissions. A permission group contains several permissions that deal with the same kinds of
access. For example, ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION are both in the LOCATION
permission group.
Table 33.1 Permission groups
Permission group Permissions
CALENDAR READ_CALENDAR, WRITE_CALENDAR
CAMERA CAMERA
CONTACTS READ_CONTACTS, WRITE_CONTACTS, GET_ACCOUNTS
LOCATION ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION
MICROPHONE RECORD_AUDIO
PHONE READ_PHONE_STATE, CALL_PHONE, READ_CALL_LOG, WRITE_CALL_LOG,
ADD_VOICEMAIL, USE_SIP, PROCESS_OUTGOING_CALLS
SENSORS BODY_SENSORS
SMS SEND_SMS, RECEIVE_SMS, READ_SMS, RECEIVE_WAP_PUSH, RECEIVE_MMS
STORAGE READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE
Once a permission is granted for any permission within the group, permission is granted for all
permissions in the group. Since both of your permissions are in the LOCATION group, you only need to
check whether you have one of those permissions.