Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request permissions from a Service in Android Marshmallow

In Android Marshmallow, permissions should be requested at runtime when they are needed, instead of all at once when an app is installed. However, I can only seem to request permissions from an Activity, which is a problem since my app contains only Services. (Why is that, you might ask? The app has an Android Wear watch face bundled inside, and all the phone does is look up photos nearby to send to the watch - no Activity needed. But it does require location permissions.)

So, is there any way to request permissions from a Service? Or somehow force the permissions to be granted at install time as in the past?

like image 653
Tony Wickham Avatar asked Aug 30 '15 02:08

Tony Wickham


People also ask

Can we request for permission from service Android?

You can check for permissions from anywhere (even from a service), based on whether the app is in foreground or background, it either shows normal dialog or generates a notification asking for permissions. The code is really easy to understand and it's really easy to use too.

How do I request permission for an Android app?

In your app's manifest file, declare the permissions that your app might need to request. Design your app's UX so that specific actions in your app are associated with specific runtime permissions. Users should know which actions might require them to grant permission for your app to access private user data.

How do I set permissions in Android programmatically?

You need to declare the permissions in the manifest file first before you request for them programmatically. refer this for more information. declaring the permission in the manifest file is static type declaration by which your os know what kind of permission your app might require.

How do I request SMS permission on Android?

In some versions of Android, this permission is turned on by default. In other versions, this permission is turned off by default. To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.


3 Answers

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

like image 91
Ali Naddaf Avatar answered Sep 19 '22 10:09

Ali Naddaf


I agree, this is very troublesome for services, I think you should report an issue on Android Developer Preview page for this.

At the moment, I think the best solution is to check for permission on service, and show notification if the permission is missing. Even better, create an DialogActivity to request for permission when users press on the notification.

like image 36
Derek Fung Avatar answered Sep 23 '22 10:09

Derek Fung


Have a look at PermissionEverywhere library. It allows you to request permission from any context.

It creates a notification clicking on which it opens up an activity asking for permission.

Sample code from library's github page:-

@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
      }
  }
like image 36
Heisenberg Avatar answered Sep 22 '22 10:09

Heisenberg