Request Location Permissions from a service Android M

39,424

Solution 1

You can not request permission via a service, since a service is not tied to a UI, this kind of makes sense. Since a service context is not an activity the exception you are getting makes sense.

You can check if the permission is available in a service and request the permission in an activity (yes you need an activity).

In a service:

 public static boolean checkPermission(final Context context) {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
 }

and in an activity:

private void showPermissionDialog() {
    if (!LocationController.checkPermission(this)) {
        ActivityCompat.requestPermissions(
            this,
            new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
            PERMISSION_LOCATION_REQUEST_CODE);
    }
}

Solution 2

You can check permissions without Activity by using application context, but you will need Activity when requesting permitions. To get app context just call to getApplicationContext() and to check permissions use ContextCompat.checkSelfPermission() instead.

Also there is good information how to use runtime permissions in correct way:

To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED, and the app has to explicitly ask the user for permission.

Edit: After you check the permissions on service, you'll need the Activity to request permission:

public static void requestPermissions (Activity activity, String[] permissions, int requestCode)
Share:
39,424
stud91
Author by

stud91

Computer Science and Computational Finance student

Updated on May 27, 2020

Comments

  • stud91
    stud91 almost 4 years

    I am using a service that on boot starts up and begins to check for location updates. Once i deny location access on permission popup now thanks to Android M my service crashes once the phone boots up.

    Since i have no activity in this case the call to requestPermissions() returns a ClassCastException as my service Context cannot be cast to an activity.

    My method call:

    ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_COARSE_LOCATION);
    

    Is there any solution so far to this OR do I have to revoke the service rights to NOT run in such a state.

  • geNia
    geNia over 8 years
    @VitaliyA The service can check for permission, but not request it, because it has no UI.
  • Bartando
    Bartando almost 8 years
    if you cannot resolve Manifest.permission.ACCESS_COARSE_LOCATION or Fine location use android prefix... like this: android.Manifest.permission.ACCESS_COARSE_LOCATION
  • OGP
    OGP over 7 years
    If required so, the service can create an overlay window with some UI (sometimes very complex) and do whatever. But this will require the Overlay permission. If the application requires 'root' almost all permissions may be granted silently.
  • pollaris
    pollaris over 6 years
    The explanation at developer.android.com/training/permissions/requesting.html#j‌​ava shows how requesting permissions causes an onRequestPermissionsResult handler to fire. This is what you need to intercept completion of the permission request.