How to put it? , Asking for permission is a very simple thing , Pass even if , Prompt on failure , Apply before you apply , The online course is so complicated , even RxPermision It's all out , After checking for a long time, you can't pay him back , We'll just use the earth method , Finally found out , The method is simpler and clearer ……

       
Permission request needs to have onRequestPermissionsResult Callback operation , So at first glance, it seems that it's complicated , In fact, the BaseActivity It's done on the top , Provide method to child Activity Just deal with it . The logic of this code is BaseActivity handle onRequestPermissionsResult Callback , And then through the way of callback , Pass on the results Acitivty.

Don't talk much , Code up :

BaseActivity Code in :
private OnBooleanListener onPermissionListener; /** *
Permission request ( There are also pitfalls in this method , If the permission is denied during detection , Then it's going to come back false, But if the authority is to ask when testing , Then, when the user is authorized, he or she selects "reject" , So the following code returns the same true, Therefore, it is necessary to use another method to detect whether the permission passes in real time )
* @param permission Manifest.permission.CAMERA * @param onBooleanListener
Authority request result callback ,true- adopt false- refuse */ public void permissionRequests(Activity
activity,String permission, OnBooleanListener onBooleanListener){
onPermissionListener=onBooleanListener;if
(ContextCompat.checkSelfPermission(activity, permission) !=
PackageManager.PERMISSION_GRANTED) {// Should we show an explanation? if
(ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_CONTACTS)) {// Permission passed onPermissionListener.onClick(true);
}else { // No authority , Apply ActivityCompat.requestPermissions(activity, new
String[]{permission},1); } }else { // Permission already exists if (onPermissionListener != null) {
onPermissionListener.onClick(true); } } } /** * When this permission is necessary , And the user didn't allow it , Allow custom settings to pop up *
@param activity * @param onBooleanListener * @param permissions */ public void
permissionCheck(Activity activity, OnBooleanListener onBooleanListener, String
...permissions){ onPermissionListener = onBooleanListener;for (String
permission:permissions){if (ContextCompat.checkSelfPermission(activity,
permission)!= PackageManager.PERMISSION_GRANTED) {// No authority , Apply
onPermissionListener.onClick(false); break; } else { if
(permission.equals(permissions[permissions.length-1])){
onPermissionListener.onClick(true); } } } } @Override public void
onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {if (requestCode == 1) { if (grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {// Permission passed if(onPermissionListener!=null){
onPermissionListener.onClick(true); } } else { // Permission denied if(onPermissionListener!=
null){ onPermissionListener.onClick(false); } } return; } super
.onRequestPermissionsResult(requestCode, permissions, grantResults); }
Activity Calling code in :
permissionRequests(Manifest.permission.CAMERA, new OnBooleanListener() {
@Override public void onClick(boolean bln) { if(bln){
ToastUtils.showToast(getContext()," Permission passed "); }else{
ToastUtils.showToast(getContext()," Permission denied "); } } });
Callback interface ( Create a new one interface) code :
package com.imxiaoyu.common.impl; /** * The callback is wrong or correct * Created by She called me Xiaoyu on
2016/11/1. */ public interface OnBooleanListener { void onClick(boolean bln); }
When to call …… That's it , You have to make a long speech , fucking ……

Finally, a detection is attached and the system settings page is skipped ( User manual authorization ) Call example of :
// Check recording rights permissionCheck(getActivity(), new OnBooleanListener() { @Override
public void onClick(boolean bln) { if (!bln) { // You do not have permission at this time final ToastPopupWindow
toastPopupWindow = new ToastPopupWindow(getActivity());
//ToastPopupWindow It's my custom dialog ()
toastPopupWindow.setContent(" Recording function must allow recording permission to use , Please go to the settings page to license the recording rights !");
toastPopupWindow.setOnClickLeftListener(" cancel ",new View.OnClickListener() {
@Override public void onClick(View v) { // cancel toastPopupWindow.dismiss(); } });
toastPopupWindow.setOnClickRightListener(" Go to the settings ", new View.OnClickListener() {
@Override public void onClick(View v) { // Go to the settings Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri =
Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri); startActivity(intent); } }); getView().post(new Runnable()
{ @Override public void run() { toastPopupWindow.show(); } }); } } },
Manifest.permission.RECORD_AUDIO);

Technology