Reputation: 1
I have tried creating a custom class for requesting permission at runtime in my android application. But the system requires us to only request from activities, so it didn't work. However with recent released support library versions i.e com.android.support:support-compat:27.0.0 onwards, a new interface PermissionCompatDelegate has been provided in ActivityCompat class with 2 method declarations boolean requestPermissions(Activity activity, String[] permissions, int requestCode) and boolean onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) . I need to know how and where to use these methods. I am intending to use these methods for custom class for requesting runtime permissions.
Edit- I have read documentation for requesting runtime permissions in android and used the said methods in an activity. I don't need to know what's already present in available documentations. I need to know if there's any way I can use these methods for my purpose .i.e creating a custom class for requesting runtime permissions.
Upvotes: 0
Views: 1027
Reputation: 1
use ActivityCompat.PermissionCompatDelegate, click here SpaPermissions.java
Upvotes: -1
Reputation: 752
You don't need to make seprate class for permission you can get Runtime Permission in your activity like this:
private void getRuntimePermissions() {
final String[] NECESSARY_PERMISSIONS = new String[] {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
if ((ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
&& (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
//Permission already granted
} else {
//ask for permission
ActivityCompat.requestPermissions(getApplicationContext(),
NECESSARY_PERMISSIONS, 123);
}
}
==>> handle(manage or execute task as per) user allow or deny permission
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 123:
if (grantResults.length > 0) {
if ((grantResults[0] == PackageManager.PERMISSION_GRANTED)
&& (grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
//Permission granted
} else {
if ((grantResults[0] == PackageManager.PERMISSION_DENIED && !ActivityCompat
.shouldShowRequestPermissionRationale(
getApplicationContext(),
Manifest.permission.CAMERA))
|| (grantResults[1] == PackageManager.PERMISSION_DENIED && !ActivityCompat
.shouldShowRequestPermissionRationale(
getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE))) {
//Permission deny with never ask again
} else {
//Permission deny
}
}
}
}
}
Upvotes: -2