1. 程式人生 > >android6.0敏感許可權動態申請

android6.0敏感許可權動態申請

    //判斷是否為android6.0系統版本,如果是,需要動態新增敏感許可權
    if (Build.VERSION.SDK_INT>=23){
        checkPopedom();
    }
    
    private void checkPopedom(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //申請 並設定回撥相應嗎
            ActivityCompat.requestPermissions(ShouYeActivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},1000);
        }
    }
    
    //Android6.0申請許可權的回撥方法
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            // requestCode即所宣告的許可權獲取碼,checkPopedom傳入
            case 1000:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲取到許可權,作相應處理
                    // init();
                } else {
                    // 沒有獲取到許可權,做特殊處理
                    // CustomToast.showToast(ShouYeActivity.this,"獲取許可權失敗,請手動開啟!");
                }
                break;
            default:
                break;
        }
    }