'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.



Solution 1:[1]

Firstly i want to tell you about Run Time Permission Check into Android M,at run time if you want granting permission through user, you need an user interaction permission dialog always open on activity or fragment (on main thread).

So you can create a method for your functionality, method that parameter are

public void yourMethod(Activty activity){

// condition apply for permission check like below

if (ContextCompat.checkSelfPermission(activity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}
}

// Now you can handle call back in your activity through overriding the method // onRequestPermissionsResult this method must be written in your activity class  

 @Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

               yourMethod(YourActivity.this);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.

              // you can also call your method here  but it may be create                          infinite loop if user deny run time permission
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Activity object also get through creating parametric construct. Create interface for method.

Solution 2:[2]

Google official library for android runtime permissions both java & kotlin

Library name: EasyPermissions

  1. Java-library

  2. Kotlin-library

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rohitashv jain
Solution 2 Manas