1. 程式人生 > >Android 動態權限申請

Android 動態權限申請

state ora wing not codes settitle ota you save

package com.dragon.android.permissionrequest;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    // 要申請的權限
    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 版本判斷。當手機系統大於 23 時,才有必要去判斷權限是否獲取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            // 檢查該權限是否已經獲取
            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
            // 權限是否已經 授權 GRANTED---授權  DINIED---拒絕
            if (i != PackageManager.PERMISSION_GRANTED) {
                // 如果沒有授予該權限,就去提示用戶請求
                showDialogTipUserRequestPermission();
            }
        }
    }

    // 提示用戶該請求權限的彈出框
    private void showDialogTipUserRequestPermission() {

        new AlertDialog.Builder(this)
                .setTitle("存儲權限不可用")
                .setMessage("由於支付寶需要獲取存儲空間,為你存儲個人信息;\n否則,您將無法正常使用支付寶")
                .setPositiveButton("立即開啟", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startRequestPermission();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(false).show();
    }

    // 開始提交請求權限
    private void startRequestPermission() {
        ActivityCompat.requestPermissions(this, permissions, 321);
    }

    // 用戶權限 申請 的回調方法
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 321) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    // 判斷用戶是否 點擊了不再提醒。(檢測該權限是否還可以申請)
                    boolean b = shouldShowRequestPermissionRationale(permissions[0]);
                    if (!b) {
                        // 用戶還是想用我的 APP 的
                        // 提示用戶去應用設置界面手動開啟權限
                        showDialogTipUserGoToAppSettting();
                    } else
                        finish();
                } else {
                    Toast.makeText(this, "權限獲取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    // 提示用戶去應用設置界面手動開啟權限

    private void showDialogTipUserGoToAppSettting() {

        dialog = new AlertDialog.Builder(this)
                .setTitle("存儲權限不可用")
                .setMessage("請在-應用設置-權限-中,允許支付寶使用存儲權限來保存用戶數據")
                .setPositiveButton("立即開啟", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 跳轉到應用設置界面
                        goToAppSetting();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(false).show();
    }

    // 跳轉到當前應用的設置界面
    private void goToAppSetting() {
        Intent intent = new Intent();

        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);

        startActivityForResult(intent, 123);
    }

    //
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 檢查該權限是否已經獲取
                int i = ContextCompat.checkSelfPermission(this, permissions[0]);
                // 權限是否已經 授權 GRANTED---授權  DINIED---拒絕
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 提示用戶應該去應用設置界面手動開啟權限
                    showDialogTipUserGoToAppSettting();
                } else {
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    Toast.makeText(this, "權限獲取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

  

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

  

Android 動態權限申請