Android之动态申请权限(API23以上需求)

本文介绍Android 6.0及以上版本中动态申请权限的方法,重点讲解了如何为应用程序添加存储权限,包括请求流程、用户提示及权限检查等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此文描述了Android之动态申请权限(API23以上需求)的文章,具体方法请看介绍

API 23之前的版本都是自动获取权限,而从 Android 6.0 开始添加了权限申请的需求,更加安全。

这里以单个存储权限为例:

· 在 Manifest 中添加访问权限:(只需设置可写,因为可写必定可读)

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

· 动态申请权限的过程:

?
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
   1 package com.dragon.android.permissionrequest;
   2
   3 import android.Manifest;
   4 import android.content.DialogInterface;
   5 import android.content.Intent;
   6 import android.content.pm.PackageManager;
   7 import android.net.Uri;
   8 import android.os.Build;
   9 import android.os.Bundle;
  10 import android.provider.Settings;
  11 import android.support.annotation.NonNull;
  12 import android.support.v4.app.ActivityCompat;
  13 import android.support.v4.content.ContextCompat;
  14 import android.support.v7.app.AlertDialog;
  15 import android.support.v7.app.AppCompatActivity;
  16 import android.widget.Toast;
  17
  18 public class MainActivity extends AppCompatActivity {
  19
  20     // 要申请的权限
  21     private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
  22     private AlertDialog dialog;
  23
  24     @Override
  25     protected void onCreate(Bundle savedInstanceState) {
  26         super.onCreate(savedInstanceState);
  27         setContentView(R.layout.activity_main);
  28
  29         // 版本判断。当手机系统大于 23 时,才有必要去判断权限是否获取
  30         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  31
  32             // 检查该权限是否已经获取
  33             int i = ContextCompat.checkSelfPermission( this , permissions[0]);
  34             // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
  35             if (i != PackageManager.PERMISSION_GRANTED) {
  36                 // 如果没有授予该权限,就去提示用户请求
  37                 showDialogTipUserRequestPermission();
  38             }
  39         }
  40     }
  41
  42     // 提示用户该请求权限的弹出框
  43     private void showDialogTipUserRequestPermission() {
  44
  45         new AlertDialog.Builder( this )
  46                 .setTitle( "存储权限不可用" )
  47                 .setMessage( "由于支付宝需要获取存储空间,为你存储个人信息;\n否则,您将无法正常使用支付宝" )
  48                 .setPositiveButton( "立即开启" , new DialogInterface.OnClickListener() {
  49                     @Override
  50                     public void onClick(DialogInterface dialog, int which) {
  51                         startRequestPermission();
  52                     }
  53                 })
  54                 .setNegativeButton( "取消" , new DialogInterface.OnClickListener() {
  55                     @Override
  56                     public void onClick(DialogInterface dialog, int which) {
  57                         finish();
  58                     }
  59                 }).setCancelable( false ).show();
  60     }
  61
  62     // 开始提交请求权限
  63     private void startRequestPermission() {
  64         ActivityCompat.requestPermissions( this , permissions, 321);
  65     }
  66
  67     // 用户权限 申请 的回调方法
  68     @Override
  69     public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int [] grantResults) {
  70         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  71
  72         if (requestCode == 321) {
  73             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  74                 if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
  75                     // 判断用户是否 点击了不再提醒。(检测该权限是否还可以申请)
  76                     boolean b = shouldShowRequestPermissionRationale(permissions[0]);
  77                     if (!b) {
  78                         // 用户还是想用我的 APP 的
  79                         // 提示用户去应用设置界面手动开启权限
  80                         showDialogTipUserGoToAppSettting();
  81                     } else
  82                         finish();
  83                 } else {
  84                     Toast.makeText( this , "权限获取成功" , Toast.LENGTH_SHORT).show();
  85                 }
  86             }
  87         }
  88     }
  89
  90     // 提示用户去应用设置界面手动开启权限
  91
  92     private void showDialogTipUserGoToAppSettting() {
  93
  94         dialog = new AlertDialog.Builder( this )
  95                 .setTitle( "存储权限不可用" )
  96                 .setMessage( "请在-应用设置-权限-中,允许支付宝使用存储权限来保存用户数据" )
  97                 .setPositiveButton( "立即开启" , new DialogInterface.OnClickListener() {
  98                     @Override
  99                     public void onClick(DialogInterface dialog, int which) {
100                         // 跳转到应用设置界面
101                         goToAppSetting();
102                     }
103                 })
104                 .setNegativeButton( "取消" , new DialogInterface.OnClickListener() {
105                     @Override
106                     public void onClick(DialogInterface dialog, int which) {
107                         finish();
108                     }
109                 }).setCancelable( false ).show();
110     }
111
112     // 跳转到当前应用的设置界面
113     private void goToAppSetting() {
114         Intent intent = new Intent();
115
116         intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
117         Uri uri = Uri.fromParts( "package" , getPackageName(), null);
118         intent.setData(uri);
119
120         startActivityForResult(intent, 123);
121     }
122
123     //
124     @Override
125     protected void onActivityResult( int requestCode, int resultCode, Intent data) {
126         super.onActivityResult(requestCode, resultCode, data);
127         if (requestCode == 123) {
128
129             if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
130                 // 检查该权限是否已经获取
131                 int i = ContextCompat.checkSelfPermission( this , permissions[0]);
132                 // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
133                 if (i != PackageManager.PERMISSION_GRANTED) {
134                     // 提示用户应该去应用设置界面手动开启权限
135                     showDialogTipUserGoToAppSettting();
136                 } else {
137                     if (dialog != null && dialog.isShowing()) {
138                         dialog.dismiss();
139                     }
140                     Toast.makeText( this , "权限获取成功" , Toast.LENGTH_SHORT).show();
141                 }
142             }
143         }
144     }
145 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值