Android more than 6.0 permission to apply for the issue

Source: Internet
Author: User
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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.