Android6.0 using the Baidu Map SDK to dynamically get location permissions

Source: Internet
Author: User

1. Cause of Error:

In the integration of Baidu Map SDK when the phone can not be located, check for no errors, Finally through the search to know is the Android version of the issue of 6.0, this is because the Android6.0 adopts the runtime permissions (runtimepermissions), Android6.0 permissions are generally divided into two kinds, a normal permission, can be directly obtained, other runtime permissions, need to prompt the user Manual consent is required to obtain.

The reason for the failure is that Xiaomi mobile phone MIUI is Android6.0.1, if not add dynamic access to the code, is not prompted, did not get permission, of course, cannot be located.

2. Resolve the code:

Private Static Final intBaidu_read_phone_state =100; Public voidshowcontacts () {if(Activitycompat.checkselfpermission ( This, Manifest.permission.ACCESS_COARSE_LOCATION)!=packagemanager.permission_granted|| Activitycompat.checkselfpermission ( This, Manifest.permission.ACCESS_FINE_LOCATION)!=packagemanager.permission_granted|| Activitycompat.checkselfpermission ( This, Manifest.permission.READ_PHONE_STATE)!=packagemanager.permission_granted) {Toast.maketext (Getapplicationcontext (),"No permissions, please manually turn on location permissions", Toast.length_short). Show (); //request one (or more) permissions and provide a fetch code (user-defined) for callback returnActivitycompat.requestpermissions (mainactivity. This,Newstring[]{manifest.permission.access_coarse_location, Manifest.permission.ACCESS_FINE_LOCATION,        Manifest.permission.READ_PHONE_STATE}, Baidu_read_phone_state); }Else{init (); }    }    //Android6.0 callback method for requesting permissions@Override Public voidOnrequestpermissionsresult (intRequestcode, string[] permissions,int[] grantresults) {        Super. Onrequestpermissionsresult (Requestcode, permissions, grantresults); Switch(requestcode) {//Requestcode is the declared permission acquisition code, which is passed in when Checkselfpermission             Casebaidu_read_phone_state:if(Grantresults[0] = =packagemanager.permission_granted) {                    //get permission to do the appropriate processing (call the Location SDK to ensure that the relevant permissions are authorized, otherwise it may cause the location to fail)init (); } Else {                    //do not get permission to do special processingToast.maketext (Getapplicationcontext (), "Get Location permissions failed, please open manually", Toast.length_short). Show (); }                 Break; default:                 Break; }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Texturemapview=(Texturemapview) Findviewbyid (R.id.mbaidumapview); //determine if the system version is android6.0, and if so, add the permissions dynamically        if(build.version.sdk_int>=23) {showcontacts (); }Else{init ();//init for positioning method        }    }

Note: This article is an excerpt from http://blog.csdn.net/shihao627/article/details/53908436, which is copyrighted.

Android6.0 using the Baidu Map SDK to dynamically get location permissions

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.