How to request permissions from a service in Android Marshmallow

In Android Marshmallow, permissions should be requested at runtime when they are needed, and not immediately when the application is installed. However, I can simply request permissions from Activity, which is a problem since my application contains only Services. (Why is this possible, you can ask? The application has the Android Wear face included in the kit, and all the phone does is look for photos nearby to send the watch - no action is required, but access permissions are required. )

So, is there a way to request permissions from the Service? Or somehow force permissions to be granted during installation, as in the past?

+90
android android-wear android-6.0-marshmallow android-permissions wear-os
Aug 30 '15 at 2:44
source share
6 answers

requestPermission() can only be called from an Activity , not a Service (unlike checkPermission() , which only requires a PackageManager ). Therefore, you need to do extra work to get around this; you need to provide Activity in your application, and, for example, your Service can check the necessary permissions and if they have not been granted, it can create a notification and can inform the user with a descriptive short message about why there is a notification and what should happen when they click on the notification, etc.

+37
Aug 30 '15 at 5:47
source share

I agree, this is very troublesome for services, I think you should report it on the Android Developer preview page.

At the moment, I believe that the best solution is to check the service permit and notify if this permission is missing. Even better, create a DialogActivity to request permission when users click on a notification.

+12
Aug 30 '15 at 3:38
source share

See PermissionEverywhere . It allows you to request permission from any context.

Creates a notification that opens a notification that requires permission.

Sample code from the github library: -

 @Override protected Boolean doInBackground(Void... params) { PermissionResponse response = PermissionEverywhere.getPermission(getApplicationContext(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE, "Notification title", "This app needs a write permission", R.mipmap.ic_launcher) .call(); //waits.. boolean isGranted = response.isGranted(); if(isGranted){ //changed from isGrante to isGranted // Do stuff } } 
+7
Jun 10 '16 at 12:19
source share

There is a very simple library that allows you to do just that. You can check the access rights from anywhere (even from the service), depending on whether the application is in the foreground or in the background, or displays a normal dialog, or generates a notification requesting permission. The code is very easy to understand and very easy to use.

Try: Android Permissions

+2
Jan 18 '17 at 16:17
source share
 @SuppressLint("NewApi") public void checkPerm() { locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if ( checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { return; } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {....} } 
0
Jul 20 '17 at 8:55
source share

You can use ResultReceiver to create a recipient for a user response, and then pass it as a callback to the Activity, via a PendingIntent notification. Link

-2
May 03 '16 at 17:43
source share



All Articles