SeikoTheWiz
SeikoTheWiz

Reputation: 883

Android v6 permissions - Asking permission for my SDK

Once again I require your help stackoverflowers!

I've been working on a SDK and now need to verify that the user granted the permissions for stuff like location or file writing.

Verifying is fine, but I think I should ask for the permission if it's needed. The thing is, I have absolutely no access to any activities. I might use the appContext to check for the permissions, but I can't listen to onRequestPermissionsResult like this.

Is there a clever way to ask for permission from my side or should I ask the developers using my SDK to ask the permissions timely so I can use the feature I (and they) need later on?

Thanks for the help!

Upvotes: 0

Views: 126

Answers (2)

Numan1617
Numan1617

Reputation: 1178

Personally I'd prefer SDKs to let me as the developer handle when to ask permissions as I may need to give the user some warning, handle it in advance or do other things before prompting them.

If you look at SDKs like Google Play Location services it leaves the handling of the location permissions to the developer and simply adds a warning to the calls that could fail by stating it may throw a SecurityException.

There is also an annotation they use called android.support.annotation.RequiresPermission which you could use to help users of your SDK. Using this annotation will give you the above mentioned Exception warning / error.

Example from official documentation:

@RequiresPermission(anyOf = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION})
public abstract Location getLastKnownLocation(String provider);

And as result

enter image description here

Upvotes: 3

Kirill Shalnov
Kirill Shalnov

Reputation: 2216

I think that you have to declare permissions into documentation and delegate permission-trouble to your sdk users, because library may be part of Model and it mustn't manipulate with UI (permission dialogs)

You can't call permission dialog without activity

Upvotes: 0

Related Questions