基于AndPermission 的权限框架封装, 记录备用

  • Post author:
  • Post category:其他


1.先依赖库:

api ‘com.yanzhenjie:permission:2.0.0-rc11’

2.封装工具类:

package com.xiao.lib.utils;

import android.app.AlertDialog.Builder;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.support.v4.app.FragmentActivity;
 import com.xiao.lib.R;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Setting;


public class PermissionUtils {

   public static final String TAG="PermissionUtils";

    public static void requestPermission(FragmentActivity activity, OnRequestPermissionListener listener, String... permissions) {
        if (Build.VERSION.SDK_INT >= VERSION_CODES.M) {

            if (AndPermission.hasPermissions(activity, permissions)) {
                if (listener != null)
                    listener.onGranted();
                return;
            }
            AndPermission.with(activity)
                    .runtime()
                    .permission(permissions)
                    .onGranted(data -> {
                        listener.onGranted();
                    })
                    .onDenied(data -> {
                        listener.onDenied(false);

                        //申请失败
                        if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {

                              new Builder(activity,R.style.Theme_AppCompat_Light_Dialog_Alert)
                                      .setTitle(R.string.tip_title)
                                      .setMessage(R.string.tip_permission_again)
                                      .setPositiveButton(R.string.btn_ok,(dialog,witch)->{
                                          requestAgain(activity,listener,permissions);

                                         })
                                      .setNegativeButton(R.string.btn_cancel,(dialog,witch)->{
                                          listener.onDenied(true);

                                        })
                                      .setCancelable(false)
                                      .create()
                                      .show();


                        }


                    })
                    .start();
        } else {
            listener.onGranted();
        }
    }

public interface OnRequestPermissionListener {
    void onGranted();

    void onDenied(boolean isAlways);

}



public static void requestAgain(FragmentActivity activity, OnRequestPermissionListener listener, String... permissions){
    // 这些权限被用户总是拒绝。
          AndPermission.with(activity)
                 .runtime()
                  .setting()
                  .onComeback(new Setting.Action() {
                      @Override
                      public void onAction() {
                          requestPermission(activity, listener, permissions);
                          // 用户从设置回来了。
                      }
                  })
                  .start();
 }

}

3.调用:

   private String permissions[]=new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};

    private void checkAppPermission() {

    PermissionUtils.requestPermission(this, new OnRequestPermissionListener() {
        @Override
        public void onGranted() {

            mBluetoothAdapter=BLEService.getInstance(getApplicationContext()).getBluetoothAdapter();

         if (mBluetoothAdapter==null||!mBluetoothAdapter.isEnabled()){
            Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(intent,REQUEST_CODE_BLE);
         }

            ToastUtils.showShort("已经授权");

        }

        @Override
        public void onDenied(boolean isAlways) {
            ToastUtils.showShort("未授权");
            if (isAlways){
                finish();
            }

        }
    },permissions);

}



版权声明:本文为sfhong2008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。