Always have permission to apply for the problem, not enough attention, has been plagued recently, forget to say, look at the code.
1, first judge the SDK version number
if (Build.VERSION.SDK_INT >= 23) {
Checkandrequestpermission ()/check Permissions
} else {
If the machine below is Android6.0, the default is to have all permissions at installation, you can call the SDK directly
}
2, @TargetApi (build.version_codes. M
private void Checkandrequestpermission () {
List lackedpermission = new ArrayList ();
if (!) ( Checkselfpermission (Manifest.permission.READ_PHONE_STATE) = = packagemanager.permission_granted)) {
Lackedpermission.add (Manifest.permission.READ_PHONE_STATE);
}
if (!) ( Checkselfpermission (Manifest.permission.WRITE_EXTERNAL_STORAGE) = = packagemanager.permission_granted)) {
Lackedpermission.add (Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!) ( Checkselfpermission (Manifest.permission.ACCESS_FINE_LOCATION) = = packagemanager.permission_granted)) {
Lackedpermission.add (Manifest.permission.ACCESS_FINE_LOCATION);
}
Permissions are already available, then directly invoke SDK
if (lackedpermission.size () = = 0) {
//handle your own business
} else {
//Request missing permissions, In Onrequestpermissionsresult, see if you get permissions, and if you get permissions, you can invoke the SDK, or don't call the SDK.
string[] requestpermissions = new string[lackedpermission.size ()];
Lackedpermission.toarray (requestpermissions);
Requestpermissions (requestpermissions, 1024);
}
}
Private Boolean hasallpermissionsgranted (int[] grantresults) {
for (int grantresult:grantresults) {
if (Grantresult = = packagemanager.permission_denied) {
return false;
}
}
return true;
}
@Override
public void onrequestpermissionsresult (int requestcode, string[] permissions, int[] grantresults) {
Super.onrequestpermissionsresult (Requestcode, permissions, grantresults);
if (Requestcode = 1024 && hasallpermissionsgranted (grantresults)) {
Fetchsplashad (this, container, ski PView, Constants.appid, Constants.splashposid, this, 0);
} else {
///If the user is not authorized, then the intention should be stated and the user should be directed to set the authorization inside. The
Toast.maketext (this,) application lacks the necessary permissions. Please click \ "Permission \" To open the required permissions. ", Toast.length_long). Show ();
Intent Intent = new Intent (settings.action_application_details_settings);
Intent.setdata (Uri.parse ("package:" + getpackagename ()));
StartActivity (Intent);
Finish ();
}
}