admin 发表于 2016-7-20 14:46:19

百度地图API解决 android 6.0定位不了的问题

原文得治:http://blog.csdn.net/qq_25817651/article/details/50913055


在于android 6.0采用了运行时权限(android-RuntimePermissions),6.0的权限一般分为两种,一种时普通权限,如下图:其他的为运行时权限


在百度地图API中,用到的权限主要有以下几个


<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 用于读取手机当前的状态 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />其中:
Manifest.permission.ACCESS_COARSE_LOCATION,
      Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE这几个为运行时权限,需要在运行时得到用户的授权。在Activity中进行授权如下:判断用户的Android版本,如果为23(6.0),执行showcontacts,提醒用户授权
if (Build.VERSION.SDK_INT>=23){
    showContacts(mMapView);
}else{
    init();
}判断用户是否已经授权,如果已经授权,直接开始定位,如果没有授权,
requestContactsPermissions(v);提示用户进行授权
public void showContacts(View v) {
    Log.i(TAG, "Show contacts button pressed. Checking permissions.");

    // Verify that all required contact permissions have been granted.
    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.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED
            || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
            != PackageManager.PERMISSION_GRANTED) {
      // Contacts permissions have not been granted.
      Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions.");
      requestContactsPermissions(v);

    } else {

      // Contact permissions have been granted. Show the contacts fragment.
      Log.i(TAG,
                "Contact permissions have already been granted. Displaying contact details.");
       init();
    }
}


private void requestContactsPermissions(View v) {
    // BEGIN_INCLUDE(contacts_permission_request)
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            || ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            || ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)
            || ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.READ_PHONE_STATE)
            ) {

      // Provide an additional rationale to the user if the permission was not granted
      // and the user would benefit from additional context for the use of the permission.
      // For example, if the request has been denied previously.
      Log.i(TAG,
                "Displaying contacts permission rationale to provide additional context.");

      // Display a SnackBar with an explanation and a button to trigger the request.
      Snackbar.make(v, "permission_contacts_rationale",
                Snackbar.LENGTH_INDEFINITE)
                .setAction("ok", new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                        ActivityCompat
                              .requestPermissions(MainActivity.this, PERMISSIONS_CONTACT,
                                        REQUEST_CONTACTS);
                  }
                })
                .show();
    } else {
      // Contact permissions have not been granted yet. Request them directly.
      ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
    }
    // END_INCLUDE(contacts_permission_request)
}


最后,对授权的结果进行判定:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode==REQUEST_CONTACTS){
      if (PermissionUtil.verifyPermissions(grantResults)) {

         init();

      } else {


      }


    }else{
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
其中 permissonutil代码如下
/**
* Utility class that wraps access to the runtime permissions API in M and provides basic helper
* methods.
*/
public abstract class PermissionUtil {

    /**
   * Check that all given permissions have been granted by verifying that each entry in the
   * given array is of the value {@link PackageManager#PERMISSION_GRANTED}.
   *
   * @see Activity#onRequestPermissionsResult(int, String[], int[])
   */
    public static boolean verifyPermissions(int[] grantResults) {
      // At least one result must be checked.
      if(grantResults.length < 1){
            return false;
      }

      // Verify that each required permission has been granted, otherwise return false.
      for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
      }
      return true;
    }

}

页: [1]
查看完整版本: 百度地图API解决 android 6.0定位不了的问题