// Acitivy application permissions
ActivityCompat.requestPermissions(activity, permissions, requestCode);
// activity authority authorization results back
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
- ActivityCompat.requestPermissions()
- For mobile phones below 6.0, the verification permissions have been declared in Manifest.xml, and the method of OnrequestPerMissionsResult () method
- For mobile phones above 6.0, hand over to Activity.RequestPerMissions () processing
public static void requestPermissions(final @NonNull Activity activity,
final @NonNull String[] permissions, final @IntRange(from = 0) int requestCode) {
if (sDelegate != null
&& sDelegate.requestPermissions(activity, permissions, requestCode)) {
// Delegate has handled the permission request.
return;
}
// 6.0 or more mobile phones
if (Build.VERSION.SDK_INT >= 23) {
// Whether the RequestPerMissionsrequestCodevalidator interface is implemented, whether the RequestCode is effective
if (activity instanceof RequestPermissionsRequestCodeValidator) {
((RequestPermissionsRequestCodeValidator) activity)
.validateRequestPermissionsRequestCode(requestCode);
}
// Call the application permissions of Activity
activity.requestPermissions(permissions, requestCode);
} else if (activity instanceof OnRequestPermissionsResultCallback) {
// Execution on the main thread
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
final int[] grantResults = new int[permissions.length];
PackageManager packageManager = activity.getPackageManager();
String packageName = activity.getPackageName();
// Check whether the permissions have been declared in the manifest.xml file
final int permissionCount = permissions.length;
for (int i = 0; i < permissionCount; i++) {
grantResults[i] = packageManager.checkPermission(
permissions[i], packageName);
}
// Reconstruction results, the process is over
((OnRequestPermissionsResultCallback) activity).onRequestPermissionsResult(
requestCode, permissions, grantResults);
}
});
}
}
2.Activity.requestPermissions()
- can only apply for one set of permissions at a time. Before a certain permission application has not been adjusted, it is not allowed to apply for permissions again.
- The page that comes with the permissions of the system comes with the permissions application process to handle it.
public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
if (requestCode < 0) {
throw new IllegalArgumentException("requestCode should be >= 0");
}
// mhascurrenTpermissionsRequest to prevent repeated application marks
if (mHasCurrentPermissionsRequest) {
Log.w(TAG, "Can request only one set of permissions at a time");
// Dispatch the callback with empty arrays which means a cancellation.
// Note here: Repeated application will get a callback immediately. The Permissions and Grantresults here are a array of 0 as 0.
// If you blindly use Permissions [0] to generate an array cross -border if you blindly use Permissions [0].
onRequestPermissionsResult(requestCode, new String[0], new int[0]);
return;
}
// getpackageManager () Get PackageManager's implementation class ApplicationPackageManager
// ApplicationPackageManager to query PMS inquiry authority management applications to PMS and use the standard ACTION value given by the system. Build an INT object of the jump system permissions management page
Intent intent = getPackageManager().buildRequestPermissionsIntent(permissions);
startActivityForResult(REQUEST_PERMISSIONS_WHO_PREFIX, intent, requestCode, null);
mHasCurrentPermissionsRequest = true;
}
3.Activity.dispathActivityResult()
- The result of the permission request is returned to the current Activity
void dispatchActivityResult(String who, int requestCode, int resultCode, Intent data,
String reason) {
if (false) Log.v(
TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode
+ ", resCode=" + resultCode + ", data=" + data);
mFragments.noteStateNotSaved();
if (who == null) {
// Familiar with the eyes
onActivityResult(requestCode, resultCode, data);
} else if (who.startsWith(REQUEST_PERMISSIONS_WHO_PREFIX)) {
who = who.substring(REQUEST_PERMISSIONS_WHO_PREFIX.length());
if (TextUtils.isEmpty(who)) {
// Return permissions processing results
dispatchRequestPermissionsResult(requestCode, data);
} else {
Fragment frag = mFragments.findFragmentByWho(who);
if (frag != null) {
dispatchRequestPermissionsResultToFragment(requestCode, data, frag);
}
}
}
}
4.Activity.dispatchRequestPermissionsResult()
private void dispatchRequestPermissionsResult(int requestCode, Intent data) {
mHasCurrentPermissionsRequest = false;
// If the package installer crashed we may have not data - best effort.
String[] permissions = (data != null) ? data.getStringArrayExtra(
PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES) : new String[0];
final int[] grantResults = (data != null) ? data.getIntArrayExtra(
PackageManager.EXTRA_REQUEST_PERMISSIONS_RESULTS) : new int[0];
// Reconstruction results, the process is over
onRequestPermissionsResult(requestCode, permissions, grantResults);
}
If repeated permissions requests are initiated, the onrequestPerMissionsResult () will be adjusted immediately. The parameters here are inconsistent with the expectations. If the callback is appropriate, it may produce an array of cross -border issues, and even stack overflow.
The process of application permissions is essentially a process of Activity jump. Then it will inevitably involve the conversion of the Activity cycle. The current activity will have a process such as onResume () -> Onpause () -> Onresume (). If you apply for permissions in onResume (), then the application of permissions will be initiated again when the result is returned, and it will eventually fall into a dead cycle.
StartActivityForresult () is a process of cross -process communication. In a request, the Activity.RequestPerMissions () method has ended after executing the startActivityForresult (). The stack frames of the correlation method will be recycled and will not cause the problem of stack overflow. Instead, it will cause a large amount of memory occupation, which is difficult to check.