Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ask for runtime permissions from a library?

We make some libraries that clients implement in their apps and now we are looking to update them to support Android 6.0 new permissions model.

My question is if there is any way to request dangerous permissions in runtime from our libraries (mostly static classes) instead of asking the client to request those permissions prior to using our libraries.

I have been messing with it but it just seems not possible for me, looks like it has to be made from an Activity which we do not have.

Am I right or is there any way to do it?

Thanks in advance.

like image 968
Imartin Avatar asked Apr 18 '16 11:04

Imartin


1 Answers

looks like it has to be made from an Activity which we do not have.

Correct. That is because the permission-request logic depends heavily on startActivityForResult() and onActivityResult(), wrapped to handle permission requests.

Plus, requesting permissions needs to be tightly tied into the overall application flow, and a UI-less library will have no way of knowing whether it is appropriate to be attempting to request a permission at this time.

You are welcome to check whether you have the permission, via ContextCompat.checkSelfPermission(), as that has no UI. So, you might use this to check, at the entry point(s) of your library, whether you have the requisite permissions to do your work, rather than just let the SecurityException or whatever happen.

like image 139
CommonsWare Avatar answered Oct 26 '22 19:10

CommonsWare