关于动态权限申请,多个动态权限申请

public final static int REQUEST_READ_PHONE_STATE = 1;

加上这个定义,忘记添加到代码片了,我的问题,这次补上了。
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //“Manifest.permission.”后面接上要申请的权限,我这里是读取权限
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            //进入到这里代表没有权限.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_PHONE_STATE);
        } else {
            //有权限,这里面添加需要权限的方法
            
        }
    }

 //添加权限,我这里是读取权限,可以不用动这段代码
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_READ_PHONE_STATE:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    //用户同意授权,这里面添加需要权限的方法
                    
                }//else 用户拒绝授权
                break;

            default:
                break;
        }
    }
    
        public void Read(View v) {
        //检查权限,将读权限换成你要申请的权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //进入到这里代表没有权限.“Manifest.permission.”后面同上一样,换成你要申请的权限

            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                //已经禁止提示了
                Toast.makeText(ContrastActivity.this, "您已禁止该权限,需要重新开启。", Toast.LENGTH_SHORT).show();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_PHONE_STATE);
            }

        } else {
            //运行需要权限的方法或语句
            
        }
    }


现在我们常常在敲代码运行app的时候,会遇到需要申请权限的问题,而我们又在AndroidMainfest.xml文件里加入了权限,这是因为在android 6.0以上需要申请动态权限,下面我将申请动态权限的格式发下,希望为和我一样遇到这个问题的人解决下问题。至于为什么要动态申请权限,你们可以百度下,有比我功力精深的讲解过,建议把危险权限看下,多注意,以后在开发中少不了多打交道的。

上面的代码就是动态申请权限的格式了,一些注意点我都注释了,新手在学习中,如果有什么地方疏漏的请指点,你们的建议和指点是我前进的明灯


下面是多个的动态申请方法,然后OnCreat方法里调用permission就行了

//权限获取
    private void permission() {
        List<String> permissionLists = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            permissionLists.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            permissionLists.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (!permissionLists.isEmpty()) {//说明肯定有拒绝的权限
            ActivityCompat.requestPermissions(this, permissionLists.toArray(new String[permissionLists.size()]), PERMISSION_REQUESTCODE);
        } else {
            Toast.makeText(this, "权限都授权了,可以搞事情了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PERMISSION_REQUESTCODE:
                if (grantResults.length > 0) {
                    for (int grantResult : grantResults) {
                        if (grantResult != PackageManager.PERMISSION_GRANTED) {
                            Toast.makeText(this, "某一个权限被拒绝了", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    //其他逻辑(这里当权限都同意的话就执行打电话、获取本地信息等逻辑)
                }
                break;
            default:
                break;
        }
    }


发布了19 篇原创文章 · 获赞 7 · 访问量 4万+
展开阅读全文

关于Android的错误:Permission Denial: 。。。.READ_EXTERNAL_STORAGE, or grantUriPermission(

10-11

这是错误代码 ,在虚拟机运行时崩溃, E/AndroidRuntime: FATAL EXCEPTION: main Process: com.add.mp3list, PID: 7193 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.add.mp3list/com.add.mp3list.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1942) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:418) at android.content.ContentResolver.query(ContentResolver.java:754) at android.content.ContentResolver.query(ContentResolver.java:704) at android.content.ContentResolver.query(ContentResolver.java:662) at com.add.mp3list.MusicList.getMusicData(MusicList.java:26) at com.add.mp3list.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Application terminated. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览