Android 适配6.0 权限检查(PermissionsUtils 相关的辅助类),8.0 ActivieAndroid安全性ContentProvider 问题

前言:最近华为应用市场要求 app targetSkdVersion 要提高到26以及26以上,在期限内可以上架26以下的app ,在期限外就不给上架 等信息,所以直接把app 升到26 (8.0).

1.适配6.0 权限检查

首先是自己写了一些权限检查的东西,相关流程就 先检查权限,然后根据结果去处理,再重写onRequestPermissionsResult

//第一步检查权限
public static boolean needRequestPerMission(Context context,String persimission){
        int checkSelfPermissionWrite = ActivityCompat.checkSelfPermission(context,persimission);

        Log.e(TAG, "敏感权限 persimission"+persimission+"检查果 checkSelfPermissionWrite=" + checkSelfPermissionWrite);
        return checkSelfPermissionWrite != PackageManager.PERMISSION_GRANTED;
    }


//第二步,没有权限的时候,申请权限(单个多个的区别就是 String... permissions 可变数组的问题)
ActivityCompat.requestPermissions(this,
                                new String[]{Manifest.permission.CALL_PHONE},
                                MY_PERMISSIONS_REQUEST_CALL_PHONE);


//第三步,处理用户操作后的后续操作

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showToast("权限已申请");
            } else {
                showToast("权限已拒绝");
            }
        }else if (requestCode == MY_PERMISSIONS_REQUEST_CALL_CAMERA){
                for (int i = 0; i < grantResults.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        //判断是否勾选禁止后不再询问
                        boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permissions[i]);
                        if (showRequestPermission) {
                            showToast("权限未申请");
                        }
                    }
                }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

发现 还是比较麻烦的,每个activity 都需要写相关的类,重复的代码太多,首先去github 上看有没有相应的框架

目前最流行的运行时权限请求框架PermissionsDispatcher、RxPermissions和easypermissions的使用和对比,看了这篇文章

有三个框架可供选择,最终选了rxJava 的PxPermissions ,简单的封装了下,因为我只需要申请权限后相关的结果,不需要前面一大堆重复的代码

--------------

下面是我的辅助类.

public class PermissionsUtils {
    private static final String TAG = PermissionsUtils.class.getSimpleName();

    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String PERMISSION_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_CALL_PHONE = Manifest.permission.CALL_PHONE;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;

    public static final String PERMISSION_ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;

    public static final String PERMISSION_WRITE_SEIINGS = Manifest.permission.WRITE_SETTINGS;


    public static final String PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    public static void requestLocationPermission(final Activity activity, final PermissionsResult result) {
        final RxPermissions rxPermissions = new RxPermissions((FragmentActivity) activity);
        rxPermissions
                .request(PERMISSION_ACCESS_FINE_LOCATION)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (result != null) {
                            result.requestPermissionsResult(aBoolean);
                        }
                    }
                });
    }

    public static void checkAndRequestCallPhonePermission(final Activity activity,final String phone){
        if(needRequestPerMission(activity,PermissionsUtils.PERMISSION_CALL_PHONE)){
            requestPermission(activity, PermissionsUtils.PERMISSION_CALL_PHONE, new PermissionsUtils.PermissionsResult() {
                @Override
                public void requestPermissionsResult(boolean isAccept) {
                    if(isAccept){
                        IntentUtils.toCall(activity, phone);
                    }else {
                        Toast.makeText(activity, R.string.no_phone_permission_string,Toast.LENGTH_LONG).show();
                    }
                }
            });
        }else {
            IntentUtils.toCall(activity, phone);
        }
    }

    public static void requestPhonePermission(Activity activity, final PermissionsResult result) {
        final RxPermissions rxPermissions = new RxPermissions((FragmentActivity) activity);
        rxPermissions
                .request(PERMISSION_READ_PHONE_STATE)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (result != null) {
                            result.requestPermissionsResult(aBoolean);
                        }
                    }
                });
    }

    public static void requestPermission(Activity activity,String permission, final PermissionsResult result){
        final RxPermissions rxPermissions = new RxPermissions((FragmentActivity) activity);
        rxPermissions
                .request(permission)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (result != null) {
                            result.requestPermissionsResult(aBoolean);
                        }
                    }
                });
    }

    /**
     * 申请单个或者多个权限,不在乎是否不再询问和哪个权限申请失败,只要有一个失败就执行失败操作:
     */
    public static void requestMutlPermission(Activity activity,final PermissionsResult result,String... permissions){
        final RxPermissions rxPermissions = new RxPermissions((FragmentActivity) activity);
        rxPermissions
                .request(permissions)
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (result != null) {
                            result.requestPermissionsResult(aBoolean);
                        }
                    }
                });
    }

    public static boolean needRequestPerMission(Context context,String persimission){
        int checkSelfPermissionWrite = ActivityCompat.checkSelfPermission(context,persimission);

        Log.e(TAG, "敏感权限 persimission"+persimission+"检查果 checkSelfPermissionWrite=" + checkSelfPermissionWrite);
        return checkSelfPermissionWrite != PackageManager.PERMISSION_GRANTED;
    }

    //回调出去
    public interface PermissionsResult {
        void requestPermissionsResult(boolean isAccept);
    }


    /**
     * https://www.cnblogs.com/Free-Thinker/p/6014765.html
     *
     * 6.0权限的基本知识,以下是需要单独申请的权限,共分为9组,每组只要有一个权限申请成功了,就默认整组权限都可以使用了。
     *
     *   group:android.permission-group.CONTACTS
     *     permission:android.permission.WRITE_CONTACTS
     *     permission:android.permission.GET_ACCOUNTS
     *     permission:android.permission.READ_CONTACTS
     *
     *   group:android.permission-group.PHONE
     *     permission:android.permission.READ_CALL_LOG
     *     permission:android.permission.READ_PHONE_STATE
     *     permission:android.permission.CALL_PHONE
     *     permission:android.permission.WRITE_CALL_LOG
     *     permission:android.permission.USE_SIP
     *     permission:android.permission.PROCESS_OUTGOING_CALLS
     *     permission:com.android.voicemail.permission.ADD_VOICEMAIL
     *
     *   group:android.permission-group.CALENDAR
     *     permission:android.permission.READ_CALENDAR
     *     permission:android.permission.WRITE_CALENDAR
     *
     *   group:android.permission-group.CAMERA
     *     permission:android.permission.CAMERA
     *
     *   group:android.permission-group.SENSORS
     *     permission:android.permission.BODY_SENSORS
     *
     *   group:android.permission-group.LOCATION
     *     permission:android.permission.ACCESS_FINE_LOCATION
     *     permission:android.permission.ACCESS_COARSE_LOCATION
     *
     *   group:android.permission-group.STORAGE
     *     permission:android.permission.READ_EXTERNAL_STORAGE
     *     permission:android.permission.WRITE_EXTERNAL_STORAGE
     *
     *   group:android.permission-group.MICROPHONE
     *     permission:android.permission.RECORD_AUDIO
     *
     *   group:android.permission-group.SMS
     *     permission:android.permission.READ_SMS
     *     permission:android.permission.RECEIVE_WAP_PUSH
     *     permission:android.permission.RECEIVE_MMS
     *     permission:android.permission.RECEIVE_SMS
     *     permission:android.permission.SEND_SMS
     *     permission:android.permission.READ_CELL_BROADCASTS
     */
}

相关调用方法;

public void requestCameraPermissions(){
        String[] permissionsArray = new String[]{PermissionsUtils.PERMISSION_CAMERA,PermissionsUtils.PERMISSION_READ_EXTERNAL_STORAGE};
        if(PermissionsUtils.needRequestPerMission(ContendPublishActivity.this,PermissionsUtils.PERMISSION_CAMERA)
                || PermissionsUtils.needRequestPerMission(ContendPublishActivity.this,PermissionsUtils.PERMISSION_READ_EXTERNAL_STORAGE)){
            PermissionsUtils.requestMutlPermission(ContendPublishActivity.this, new PermissionsUtils.PermissionsResult() {
                @Override
                public void requestPermissionsResult(boolean isAccept) {
                    if(!isAccept) {
                        Toast.makeText(ContendPublishActivity.this, R.string.no_camera_permission_string, Toast.LENGTH_SHORT).show();
                    }else {
                        PhotoUtils.gotoChooseMulti(ContendPublishActivity.this, MAX_PHOTO_COUNT - photos.size());
                    }
                }
            },permissionsArray);
        }else {
            PhotoUtils.gotoChooseMulti(ContendPublishActivity.this, MAX_PHOTO_COUNT - photos.size());
        }
    }

最后发现现在市场的大多数的app 在启动页面的时候就开始把所有的权限都申请一遍,然后再进入主页,所有也参照了这种处理方式,在需要的地方在进行权限检查即可,有权限就执行,没权限调用辅助类申请。

 

2.8.0 ActivieAndroid安全性ContentProvider 问题

以前时候了ActiveAndroid 的数据库,然后再8.0手机上面闪退,下面是处理方案

https://github.com/pardom-zz/ActiveAndroid/issues/536#issuecomment-344470558

问题:

Android 适配6.0 权限检查(PermissionsUtils 相关的辅助类),8.0 ActivieAndroid安全性ContentProvider 问题_第1张图片

解决方案:

Android 适配6.0 权限检查(PermissionsUtils 相关的辅助类),8.0 ActivieAndroid安全性ContentProvider 问题_第2张图片

注意点:

//这里的name 的自己建的Provider 的路径一定要是自己项目的全路径,
//成功的标示就是能够点入到对应的类即可
//authorities 值对应自己想买的完整的包名

结束语:虽然网上轮子很多,但需要选择自己需要的,还需要进一步的封装,毕竟每个人的需求不一样,不能简单的复制粘贴 还是需要自己手动敲代码和动脑子思考的.

我只是简单的记录了下遇到的问题,最后祝各位 适配顺利,

你可能感兴趣的:(android-杂记)