Android 6.0 动态权限申请管理 用户拒绝不再提示后再申请

对于targetSdkVersion版本为23包括23以上,在6.0以上机型运行的时候都需要进行动态权限的申请,但并不是每个权限都需要,因此看下图 下图的权限为危险权限 目前为9组27个

 
  
下面是demo的效果图:
在闪屏页启动的时候一次性获取所有权限
 
  
 
  
当用户拒绝之后会再次申请去系统设置页面来引导用户给予必要的权限~
 
  
如果用户在设置页没有给予权限,或者拒绝的同时点击了不再询问那,么在启动页弹出dialog再次引导过去~
 
  
 
  
下面是代码展示,关键地方会有注释
 
  
首先,即使是动态权限申请,也需要在清单文件中声明才行
 
  
 
  
   
    


 
  
这是闪屏页的Activity
 
  
public class SplashActivity extends AppCompatActivity {

    private Intent i;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    timeTv.setText((Integer) msg.obj + "");
                    break;
                case 2:
                    startActivity(i);
                    finish();
                    break;
            }
        }
    };
    private TextView timeTv;
    private boolean hasrefuse;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        i = new Intent();
        i.setClass(this, MainActivity.class);
        timeTv = (TextView) findViewById(R.id.time_tv);
        ImageView splash_iv = (ImageView) findViewById(R.id.splash_iv);
        Glide.with(this).load("http://cn.bing.com/az/hprichbg/rb/Motherboard_ROW13196131750_1920x1080.jpg")
                .into(splash_iv);
        
        //判断用户是否给这些权限授权
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            
            //判断是否拒绝过
            hasrefuse = ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.CALL_PHONE);
            hasrefuse = ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.READ_CONTACTS);
            if(hasrefuse){
                //当拒绝了授权后,为提升用户体验,可以以弹窗的方式引导用户到设置中去进行设置
                new AlertDialog.Builder(SplashActivity.this)
                        .setMessage("需要开启权限才能使用此功能")
                        .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //引导用户到设置中去进行设置
                                Intent intent = new Intent();
                                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                intent.setData(Uri.fromParts("package", getPackageName(), null));
                                startActivity(intent);

                                finish();

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        .create()
                        .show();
            }else {
                //如果没有拒绝过,进入回调
                ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_CONTACTS, android.Manifest.permission.CALL_PHONE}, 1);
            }
        } 
        //如果权限都申请过,开始倒计时
        else {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    //引导页倒计时
                    for (int j = 5; j > 0; j--) {
                        try {
                            Thread.sleep(1000);
                            Message msg = Message.obtain();
                            msg.what = 1;
                            msg.obj = j;
                            handler.sendMessage(msg);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    handler.sendEmptyMessage(2);
                }
            }.start();
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED&&
                        grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
                    startActivity(i);
                    finish();
                } else {
                    Toast.makeText(this, "请给与权限", Toast.LENGTH_SHORT).show();

                    //引导用户到设置中去进行设置
                    Intent intent = new Intent();
                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    intent.setData(Uri.fromParts("package", getPackageName(), null));
                    startActivity(intent);

                    finish();
                }
                break;
        }
    }

}


 
  
 
 

你可能感兴趣的:(Android 6.0 动态权限申请管理 用户拒绝不再提示后再申请)