android6.0 權限管理 方案

能找到這篇文章,說明你對android6.0權限已有所瞭解。介紹權限的文章滿天飛,我就不介紹了。這裏主要介紹2個問題。彈窗流程和微信的同樣,你們打開微信能夠試試。
一、如何知道已經得到權限?
二、系統第二次彈出權限詢問對話框,用戶勾選再也不提示的複選框後,若是引導用戶賦予應用權限?java

這裏使用到的開源庫是android

compile 'com.anthonycr.grant:permissions:1.0'

請自行加入到gradle裏微信

0、先寫個得到權限的回調方法,用戶容許或者拒絕都會調用該方法ide

注意,該回調必需要加,不然開源庫的onGranted()和onDenied(String permission)將不會調用gradle

//是否顯示本身定義的dialog,系統權限dialog彈出後用戶勾選不在提示,該參數將起做用
protected boolean isShowMyPermissionsDialog = false;    


@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (grantResults.length > 0){
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Do the stuff that requires permission...
                isShowMyPermissionsDialog=false;
            }else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
                // Should we show an explanation?
                isShowMyPermissionsDialog=true;
            }
        }
        //notify
        PermissionsManager.getInstance().notifyPermissionsChange(permissions,grantResults);
    }

一、  開始檢查是否有權限ui

private void checkSdcardPermission() {
        //檢查你是否有READ_PHONE_STATE權限
        if (!PermissionsManager.getInstance().hasPermission(this, Manifest.permission.READ_PHONE_STATE)) {
//shouldShowRequestPermissionRationale()默認爲false
//第一次請求權限時,用戶拒絕了,下一次:shouldShowRequestPermissionRationale()返回 true
//第二次請求權限時,用戶拒絕了,並選擇了「不在提醒」的選項時:shouldShowRequestPermissionRationale()返回 false
            if (ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) && isShowMyPermissionsDialog) {
                //顯示自定義對話框,說明爲啥須要這個權限
                showMyPermissionsDialog();
            } else {
                //顯示系統自帶的權限詢問對話框
                showSystemPermissionsDialog();
            }
            return;
        }
        //已得到權限,讀取想要的信息
        ......
        startActivity();

    }

2. 自定義權限詢問對話框this

private void showMyPermissionsDialog() {
        showMessageOKCancel("在設置-應用-xx-權限中開啓xxx權限,以正常使用xx功能",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                //跳轉設置頁
                                goToAppSettings();
                                break;
                            case DialogInterface.BUTTON_NEGATIVE:
                                //取消
                                finish();
                                break;
                        }

                    }
                });
    }


3. 系統自動權限對話框spa

private void showSystemPermissionsDialog() {
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(SplashActivity.this,
                new String[]{Manifest.permission.READ_PHONE_STATE}, new PermissionsResultAction() {
                    @Override
                    public void onGranted() {
                        MLog.d("已得到READ_PHONE_STATE權限");
                        //已得到權限,讀取想要的信息
                        ......
                    }

                    @Override
                    public void onDenied(String permission) {
                        MLog.d("沒有得到READ_PHONE_STATE權限");
                        showMyPermissionsDialog();
                    }
                });
    }

4.  跳轉至應用設置頁code

private static final int REQUEST_APP_SETTINGS = 1;
    /**
     * 應用設置頁
     */
    private void goToAppSettings() {
        Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName()));
        myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
//        myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(myAppSettings, REQUEST_APP_SETTINGS);
    }


5.  用戶在應用設置頁設置完權限後回到應用,判斷是否已得到權限get

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_APP_SETTINGS) {

            if (PermissionsManager.getInstance().hasPermission(this, Manifest.permission.READ_PHONE_STATE)) {
                Toast.makeText(this, "READ_PHONE_STATE permissions granted!", Toast.LENGTH_SHORT).show();
                //已得到權限,讀取想要的信息
                ......
                startActivity();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
private void showMessageOKCancel(String message, DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(SplashActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", listener)
                .setNegativeButton("Cancel", listener)
                .create()
                .show();
    }
相關文章
相關標籤/搜索