AnxGotta AnxGotta - 1 month ago 6
Android Question

Does the Android 6.0 runtime permission check callback have to be handled in the Activity?

Basically, I want to create a wrapper for my application to handle the runtime permission checks using my own callback. I want to do some equivalent of adding a new callback to the

requestPermissions
method or Overriding the
Activity
method
onRequestPermissionsResult
in this function so I don't have to worry about application states. Is this possible?

I know that there are other ways to manage this using the
requestCode
and
Fragment
s but I'm just curious about this as a possibility.

See example:

interface IPermissionResponse {
void permissionGranted();
void providePermissionRationale();
void permissionDenied();
}

private final static int REQUEST_INTERNET = 1;

@SuppressLint("NewApi")
public static void checkInternetPermission(Activity activity, boolean giveRationale, IPermissionResponse callback) {

if(!platformVersionCheck()){
// not android M, proceed with permission granted
callback.permissionGranted();
return;
}

Context nContext = activity.getApplicationContext();

// android M things...
if(nContext.checkSelfPermission(Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED){
// permission granted
callback.permissionGranted();
} else {
// permission not granted

// provide additional rationale to the user if the permission was not granted
// and the user would benefit from additional information about the use of the permission
if(giveRationale && activity.shouldShowRequestPermissionRationale(Manifest.permission.INTERNET)){
callback.providePermissionRationale();
}

// request the permission
activity.requestPermissions(new String[] {Manifest.permission.INTERNET}, REQUEST_INTERNET
/*, new IRequestPermissionCallback(){
// create new callback here
// if accepted, callback.permissionGranted
// if denied, callback.permissionDenied
}*/);

// OR SOMETHING LIKE

// handle the result callback ?????
// activity.onRequestPermissionsResult(........){
// // create new callback here
// // if accepted, callback.permissionGranted
// // if denied, callback.permissionDenied
// };
}
}

private static boolean platformVersionCheck() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

Answer

No, there isn't any callback. To have an automatic management you can look here: Permission library