关闭

android6.0运行时权限完美封装

标签: android 6.0权限运行时权限android 6.0运行时android 运行时权限
1104人阅读 评论(1) 收藏 举报
分类:

前几天看了郭大神的运行时权限的专讲,深受启发,由于现在基于目前项目中的运行时权限封装的还不是那么完美,趁着郭神建议的还是历历在目。于是把它完整的敲了下来。并在此基础上添加上自己的一些见解,封装成一个完整的demo,希望与大家进行交流与。


在这里我进行了简单的在activity中简单进行获取权限和工具类封:


某一个权限被禁止或者是所有的权限被禁止,这里我做了去到烯烃界面去设置的处理: 


如果要开启多个权限的话,则可能不同的手机显示的效果不同,有些手机在权限Dialog上显示需要获取权限的个数,但有些手机需要多少个权限显示多少个。

最后一步处理:如果某个权限被禁止的话,则到设置界面去提示用户去打开权限:


部分代码:

/**
 * 类功能描述:在activity中进行检测权限的简单方法 </br>
 * 博客地址:http://blog.csdn.net/androidstarjack
 * @author 老于
 * @version 1.0 </p> 修改时间:</br> 修改备注:</br>
 */
public class CheckPermissionOnActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkpermission1);
        ButterKnife.bind(this);
    }
    @OnClick({R.id.btn_runtimne_onClick, R.id.btn_runtimne_onClick_multify,R.id.btn_runtimne_onClick_seting})
    public void onMyListener(View v){
        switch (v.getId()){
            case R.id.btn_runtimne_onClick:
                requestToUserPersiom(1);
                break;
            case R.id.btn_runtimne_onClick_multify:
                requestToUserPersiom(2);
                break;
            case R.id.btn_runtimne_onClick_seting:
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:"+ getPackageName()));
                startActivity(intent);
                break;
        }
    }
    public void requestToUserPersiom(int permisionIndexCount){
        switch (permisionIndexCount){
            case 1:
                ActivityCompat.requestPermissions(CheckPermissionOnActivity.this, new String[]{android.Manifest.permission.CALL_PHONE},100);//去请求权限
                break;
            case 2:
            case 3:
            case 4:
                ActivityCompat.requestPermissions(CheckPermissionOnActivity.this, new String[]{android.Manifest.permission.CALL_PHONE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
                break;

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 100){
            if(grantResults.length >0 ){
                for (int i = 0; i < grantResults.length; i++) {
                    if( grantResults[i] == PackageManager.PERMISSION_DENIED){//拒绝啦
                        return ;
                    }
                }
                doosmething();
            }
        }
    }

    /**
     * 做其他事情
     */
    public void doosmething(){
        GetToast.useString(CheckPermissionOnActivity.this,"开始做其他事情了");
    }
}
封装成BaseActivitgy工具类:

public class BasePermisitionActivity extends Activity {

    private static final int REQUESTPERMISSION_CODE = 100;
    private static QuestPermissionListener questPermissionListener;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.addPermissionActivty(this);
    }

    public static  void requestRuntimePermission(String [] permissions,QuestPermissionListener questPermissionListener){
        Activity actyivty = ActivityManager.permissionActivilyList.get(ActivityManager.permissionActivilyList.size() -1);
        if(actyivty == null){
            return;
        }
        List<String> neetRequestPermisList  = new ArrayList<String>();
        for (String permission : permissions) {
            if(ActivityCompat.checkSelfPermission(actyivty,permission) == PackageManager.PERMISSION_DENIED){
                neetRequestPermisList.add(permission);
            }
        }
        BasePermisitionActivity.questPermissionListener  = questPermissionListener;
        if(!neetRequestPermisList.isEmpty()){
            ActivityCompat.requestPermissions(actyivty,neetRequestPermisList.toArray(new String[neetRequestPermisList.size()]),REQUESTPERMISSION_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == REQUESTPERMISSION_CODE){
            for (int i = 0; i < grantResults.length; i++) {
                int granrRequest = grantResults[i];
                if(granrRequest == PackageManager.PERMISSION_DENIED){//权限被拒绝啦
                    if(questPermissionListener != null){
                        questPermissionListener.denySomePermission();
                    }
                        return;
                }
            }
            questPermissionListener.doAllPermissionGrant();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    public void onResumeCheckPermission(String ...permission){
        if(PermisionControl.lackMissPermission(this,permission)){
            showMissPermissionDialog();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.removePermissionActiivty(this);
    }
    /**
     * 丢失权限,进行弹框设置
     */
    public void showMissPermissionDialog() {
        if (builder == null) {
            builder = new AlertDialog.Builder(BasePermisitionActivity.this);
            builder.setTitle("帮助");
            String msg = "当前应用缺少必要权限。<br>\r请点击\"设置\"-\"权限\"-打开所需权限。<br>\r\r最后点击两次后退按钮,即可返回。";
            builder.setMessage(Html.fromHtml(msg));
            //materialDialog.setMovementMethod(LinkMovementMethod.getInstance());
            builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                }
            });
            builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    startAppSettings();
                }
            });
            alertDialog = builder.create();
            alertDialog.show();
        } else {
            alertDialog.show();
        }
    }
    // 启动应用的设置
    public void startAppSettings() {
        try {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:"+ getPackageName()));
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

与其相关的工具类:

/**
 * 类功能描述:6.0运行时权限 </br>
 * permission权限控制器</br>
 * 博客地址:http://blog.csdn.net/androidstarjack
 * @author 老于
 * Created  on 2017/1/3/002
 * @version 1.0 </p> 修改时间:</br> 修改备注:</br>
 */
public class PermisionControl {
    /**
     * 检查所有的权限是否被禁止
     */
    public static boolean lackMissPermission(Context cnt ,String... permission){
        boolean relust = true ;
        for (String per : permission) {
            if(ActivityCompat.checkSelfPermission(cnt,per) == PackageManager.PERMISSION_DENIED){
                relust = false ;
                break;
            }
        }
        return relust;
    }

    /**
     * 请求运行时权限
     * eg:
     */
    public void requestRuntimePermission(QuestPermissionListener questPermissionListener, String... permissions){
        BasePermisitionActivity.requestRuntimePermission(permissions,questPermissionListener);
    }
}

以及其他的一些封装:

public class PermissionUsage {
    /**********权限:打电话,写入内存卡,定位********/
    public static String[] LOACATION_CAMERA = { Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE,
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE};
    /**********权限:读取内存卡********/
    public static String[] READ_EXTRASORE = { Manifest.permission.READ_EXTERNAL_STORAGE};
    /**********权限:打电话权限********/
    public static String[] RCALL_PHONE = { Manifest.permission.READ_EXTERNAL_STORAGE};
}

QQ交流群 :232203809,欢迎入群

微信公众号:终端研发部
(欢迎关注学习和交流) 






1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:228676次
    • 积分:3863
    • 等级:
    • 排名:第8223名
    • 原创:147篇
    • 转载:16篇
    • 译文:0篇
    • 评论:160条
    最新评论