Android 6.0及以上版本定位权限开发问题

在开发APP过程中,发现Android 6.0版本的百度定位无法使用,查阅资料后发现android 6.0及以上系统 权限管理变了,定位属于隐私权限,需要在运行时手动申请

关于Android5.0和Android6.0在权限方面的区别:

Android5.0

对于6.0以下的权限申请,需要在AndroidManifest.xml中进行相关配置
配置示例:


    <permission android:name="android.permission.ACCESS_COARSE_LOCATION">permission>
    
    <permission android:name="android.permission.ACCESS_FINE_LOCATION">permission>
    
    <permission android:name="android.permission.ACCESS_WIFI_STATE">permission>
    
    <permission android:name="android.permission.ACCESS_NETWORK_STATE">permission>
    
    <permission android:name="android.permission.CHANGE_WIFI_STATE">permission>
    
    <permission android:name="android.permission.READ_PHONE_STATE">permission>
    
    <permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">permission>
    
    <permission android:name="android.permission.INTERNET" />
    
    <permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">permission>

这些权限都是在安装之前需要你确认申请的权限信息,安装完成之后,手机的这些权限都开启,因此定位服务可以正常使用

Android6.0

对于Android6.0,因为其权限修改为动态申请,因此在安装完成启动APP之后,进入某些需要权限的页面Activity,手机会弹出提示框,进行权限申请,这个部分就是我们代码需要完成的部分
在CustomApplication中添加getPermissionList方法:

public List getPermissionList(Activity activity){
        List permission = new ArrayList<>();
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.ACCESS_FINE_LOCATION);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.ACCESS_COARSE_LOCATION);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.READ_PHONE_STATE);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.ACCESS_WIFI_STATE)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.ACCESS_WIFI_STATE);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.ACCESS_NETWORK_STATE)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.ACCESS_NETWORK_STATE);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.INTERNET)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.INTERNET);
        if(ContextCompat.checkSelfPermission(activity, com.bupt.smart.smart.Manifest.permission.CHANGE_WIFI_STATE)!= PackageManager.PERMISSION_GRANTED)
            permission.add(com.bupt.smart.smart.Manifest.permission.CHANGE_WIFI_STATE);
        return permission;
    }

当你的Activty或者Fragment需要定位时:
首先自定义一个权限获取码,用于回调函数中做对应处理
private final int SDK_PERMISSION_REQUEST = 1;

//判断权限
/**
     *  onClick 集中处理函数
     *  @param v
     * */
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //跳转到地址页面
            case R.id.iv_upperlower:{
                intent = new Intent(this.getActivity(), LocationActivity.class);
                this.startActivity(intent);
                //this.getActivity().finish();
            }break;

            //刷新地址
            case R.id.iv_refresh:{
                //判断权限
                List permission = application.getPermissionList(getActivity());
                if(permission.size()>0){
                // 申请一个(或多个)权限,并提供用于回调返回的获取码(用户定义)
                   requestPermissions(permission.toArray(new String[permission.size()]),SDK_PERMISSION_REQUEST);
                    return;
                }
                mLocationClient.stop();
                mLocationClient.start();

            }break;
        }
    }

在Activity的onRequestPermissionResult回调函数中做处理:

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
        switch (requestCode){
        // requestCode即所声明的权限获取码
            case SDK_PERMISSION_REQUEST:{
                mLocationClient.stop();
                mLocationClient.start();
            }break;
        }
    }

这样的权限申请只需要一次,之后启动该APP不需要再申请
权限添加越全越好!!!
很有可能因为某个看起来不是很重要的权限缺失导致百度地图无法使用

你可能感兴趣的:(安卓)