Android6.0运行时权限。

举报
yechaoa 发表于 2022/05/30 23:26:46 2022/05/30
【摘要】 一、集成三方库PermissionsDispatcher 1.添加依赖 a. project目录下的build.gradle文件 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'b. app目录下的build.gradle文件 com...



一、集成三方库PermissionsDispatcher



1.添加依赖

a. project目录下的build.gradle文件

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
 


  
  1. compile 'com.github.hotchemi:permissionsdispatcher:2.2.0'
  2. apt 'com.github.hotchemi:permissionsdispatcher-processor:2.2.0'


2.给Activity或者Fragment注册


  
  1. @RuntimePermissions
  2. public class LoginActivity extends AppCompatActivity{


3.实现五个方法


  
  1. /**
  2. * 处理当用户允许该权限时需要处理的方法(括号里面是当前需要请求的权限)
  3. */
  4. @NeedsPermission(Manifest.permission.READ_PHONE_STATE)
  5. void doACacheNeedsPermission() {
  6. //do something
  7. }
  8. @OnShowRationale(Manifest.permission.READ_PHONE_STATE)
  9. void ACacheShowRationale(PermissionRequest request) {
  10. request.proceed(); // 提示用户权限使用的对话框
  11. }
  12. @OnNeverAskAgain(Manifest.permission.READ_PHONE_STATE)
  13. void ACacheOnNeverAskAgain() {
  14. new AlertDialog.Builder(this)
  15. .setPositiveButton("重新设置", new DialogInterface.OnClickListener() {
  16. @Override
  17. public void onClick(@NonNull DialogInterface dialog, int which) {
  18. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  19. intent.setData(Uri.parse("package:" + getPackageName()));
  20. startActivity(intent);
  21. }
  22. })
  23. .setNegativeButton("取消", null)
  24. .setCancelable(false)
  25. .setMessage("您已拒绝授予权限")
  26. .show();
  27. }
  28. /**
  29. * 如果用户拒绝该权限执行的方法
  30. */
  31. @OnPermissionDenied(Manifest.permission.READ_PHONE_STATE)
  32. void ACacheOnPermissionDenied() {
  33. MyUtil.showToast("真的确定拒绝授予权限吗");
  34. }
  35. /**
  36. * 权限请求回调,提示用户之后,用户点击“允许”或者“拒绝”之后调用此方法
  37. * @param requestCode 定义的权限编码
  38. * @param permissions 权限名称
  39. * @param grantResults 允许/拒绝
  40. */
  41. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  42. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  43. LoginActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
  44. }


比如是一个点击事件


  
  1. case R.id.btn:
  2. //6.0权限
  3. LoginActivityPermissionsDispatcher.doACacheNeedsPermissionWithCheck(this);
  4. break;


二、Android6.0M权限管理实战,完美轻量级封装


1.第一步:检测所有的权限是否都已授权


  
  1. /**
  2. * 检测所有的权限是否都已授权
  3. *
  4. * @param permissions
  5. * @return
  6. */
  7. private boolean checkPermissions(String[] permissions) {
  8. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  9. return true;
  10. }
  11. for (String permission : permissions) {
  12. if (ContextCompat.checkSelfPermission(this, permission) !=
  13. PackageManager.PERMISSION_GRANTED) {
  14. return false;
  15. }
  16. }
  17. return true;
  18. }

2.第二步:获取权限集中需要申请权限的列表


  
  1. /**
  2. * 获取权限集中需要申请权限的列表
  3. *
  4. * @param permissions
  5. * @return
  6. */
  7. private List<String> getDeniedPermissions(String[] permissions) {
  8. List<String> needRequestPermissionList = new ArrayList<>();
  9. for (String permission : permissions) {
  10. if (ContextCompat.checkSelfPermission(this, permission) !=
  11. PackageManager.PERMISSION_GRANTED ||
  12. ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
  13. needRequestPermissionList.add(permission);
  14. }
  15. }
  16. return needRequestPermissionList;
  17. }


  
  1. /**
  2. * 请求权限
  3. *
  4. * @param permissions 请求的权限
  5. * @param requestCode 请求权限的请求码
  6. */
  7. public void requestPermission(String[] permissions, int requestCode) {
  8. this.REQUEST_CODE_PERMISSION = requestCode;
  9. if (checkPermissions(permissions)) {
  10. permissionSuccess(REQUEST_CODE_PERMISSION);
  11. } else {
  12. List<String> needPermissions = getDeniedPermissions(permissions);
  13. ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
  14. }
  15. }


  
  1. /**
  2. * 系统请求权限回调
  3. *
  4. * @param requestCode
  5. * @param permissions
  6. * @param grantResults
  7. */
  8. @Override
  9. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  10. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  11. if (requestCode == REQUEST_CODE_PERMISSION) {
  12. if (verifyPermissions(grantResults)) {
  13. permissionSuccess(REQUEST_CODE_PERMISSION);
  14. } else {
  15. permissionFail(REQUEST_CODE_PERMISSION);
  16. showTipsDialog();
  17. }
  18. }
  19. }


  
  1. /**
  2. * 确认所有的权限是否都已授权
  3. *
  4. * @param grantResults
  5. * @return
  6. */
  7. private boolean verifyPermissions(int[] grantResults) {
  8. for (int grantResult : grantResults) {
  9. if (grantResult != PackageManager.PERMISSION_GRANTED) {
  10. return false;
  11. }
  12. }
  13. return true;
  14. }


  
  1. /**
  2. * 获取权限成功
  3. *
  4. * @param requestCode
  5. */
  6. public void permissionSuccess(int requestCode) {
  7. Log.d(TAG, "获取权限成功=" + requestCode);
  8. }


  
  1. /**
  2. * 权限获取失败
  3. * @param requestCode
  4. */
  5. public void permissionFail(int requestCode) {
  6. Log.d(TAG, "获取权限失败=" + requestCode);
  7. }
  8. /**
  9. * 显示提示对话框
  10. */
  11. private void showTipsDialog() {
  12. new AlertDialog.Builder(this)
  13. .setTitle("提示信息")
  14. .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
  15. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  16. @Override
  17. public void onClick(DialogInterface dialog, int which) {
  18. }
  19. })
  20. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  21. @Override
  22. public void onClick(DialogInterface dialog, int which) {
  23. startAppSettings();
  24. }
  25. }).show();
  26. }


  
  1. /**
  2. * 启动当前应用设置页面
  3. */
  4. private void startAppSettings() {
  5. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  6. intent.setData(Uri.parse("package:" + getPackageName()));
  7. startActivity(intent);
  8. }



  
  1. package cn.bluemobi.dylan.mpermissions;
  2. import android.content.DialogInterface;
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.provider.Settings;
  8. import android.support.annotation.NonNull;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v4.content.ContextCompat;
  11. import android.support.v7.app.AlertDialog;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.util.Log;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. /**
  17. * Created by yuandl on 2016-11-16.
  18. */
  19. public class MPermissionsActivity extends AppCompatActivity {
  20. private final String TAG = "MPermissions";
  21. private int REQUEST_CODE_PERMISSION = 0x00099;
  22. /**
  23. * 请求权限
  24. *
  25. * @param permissions 请求的权限
  26. * @param requestCode 请求权限的请求码
  27. */
  28. public void requestPermission(String[] permissions, int requestCode) {
  29. this.REQUEST_CODE_PERMISSION = requestCode;
  30. if (checkPermissions(permissions)) {
  31. permissionSuccess(REQUEST_CODE_PERMISSION);
  32. } else {
  33. List<String> needPermissions = getDeniedPermissions(permissions);
  34. ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
  35. }
  36. }
  37. /**
  38. * 检测所有的权限是否都已授权
  39. *
  40. * @param permissions
  41. * @return
  42. */
  43. private boolean checkPermissions(String[] permissions) {
  44. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  45. return true;
  46. }
  47. for (String permission : permissions) {
  48. if (ContextCompat.checkSelfPermission(this, permission) !=
  49. PackageManager.PERMISSION_GRANTED) {
  50. return false;
  51. }
  52. }
  53. return true;
  54. }
  55. /**
  56. * 获取权限集中需要申请权限的列表
  57. *
  58. * @param permissions
  59. * @return
  60. */
  61. private List<String> getDeniedPermissions(String[] permissions) {
  62. List<String> needRequestPermissionList = new ArrayList<>();
  63. for (String permission : permissions) {
  64. if (ContextCompat.checkSelfPermission(this, permission) !=
  65. PackageManager.PERMISSION_GRANTED ||
  66. ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
  67. needRequestPermissionList.add(permission);
  68. }
  69. }
  70. return needRequestPermissionList;
  71. }
  72. /**
  73. * 系统请求权限回调
  74. *
  75. * @param requestCode
  76. * @param permissions
  77. * @param grantResults
  78. */
  79. @Override
  80. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  81. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  82. if (requestCode == REQUEST_CODE_PERMISSION) {
  83. if (verifyPermissions(grantResults)) {
  84. permissionSuccess(REQUEST_CODE_PERMISSION);
  85. } else {
  86. permissionFail(REQUEST_CODE_PERMISSION);
  87. showTipsDialog();
  88. }
  89. }
  90. }
  91. /**
  92. * 确认所有的权限是否都已授权
  93. *
  94. * @param grantResults
  95. * @return
  96. */
  97. private boolean verifyPermissions(int[] grantResults) {
  98. for (int grantResult : grantResults) {
  99. if (grantResult != PackageManager.PERMISSION_GRANTED) {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * 显示提示对话框
  107. */
  108. private void showTipsDialog() {
  109. new AlertDialog.Builder(this)
  110. .setTitle("提示信息")
  111. .setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
  112. .setNegativeButton("取消", new DialogInterface.OnClickListener() {
  113. @Override
  114. public void onClick(DialogInterface dialog, int which) {
  115. }
  116. })
  117. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  118. @Override
  119. public void onClick(DialogInterface dialog, int which) {
  120. startAppSettings();
  121. }
  122. }).show();
  123. }
  124. /**
  125. * 启动当前应用设置页面
  126. */
  127. private void startAppSettings() {
  128. Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  129. intent.setData(Uri.parse("package:" + getPackageName()));
  130. startActivity(intent);
  131. }
  132. /**
  133. * 获取权限成功
  134. *
  135. * @param requestCode
  136. */
  137. public void permissionSuccess(int requestCode) {
  138. Log.d(TAG, "获取权限成功=" + requestCode);
  139. }
  140. /**
  141. * 权限获取失败
  142. * @param requestCode
  143. */
  144. public void permissionFail(int requestCode) {
  145. Log.d(TAG, "获取权限失败=" + requestCode);
  146. }
  147. }

1.布局文件activity_main.xml


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. android:paddingLeft="@dimen/activity_horizontal_margin"
  10. android:paddingRight="@dimen/activity_horizontal_margin"
  11. android:paddingTop="@dimen/activity_vertical_margin"
  12. tools:context="cn.bluemobi.dylan.mpermissions.MainActivity">
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Hello World!" />
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:onClick="onClick1"
  22. android:text="打电话" />
  23. <Button
  24. android:id="@+id/button2"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:onClick="onClick2"
  28. android:text="写SD卡" />
  29. <Button
  30. android:id="@+id/button3"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:onClick="onClick3"
  34. android:text="拍照" />
  35. </LinearLayout>


  
  1. package cn.bluemobi.dylan.mpermissions;
  2. import android.Manifest;
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. public class MainActivity extends MPermissionsActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. }
  13. /**
  14. * 打电话
  15. *
  16. * @param view
  17. */
  18. public void onClick1(View view) {
  19. requestPermission(new String[]{Manifest.permission.CALL_PHONE}, 0x0001);
  20. }
  21. /**
  22. * 写SD卡
  23. *
  24. * @param view
  25. */
  26. public void onClick2(View view) {
  27. requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x0002);
  28. }
  29. /**
  30. * 拍照
  31. *
  32. * @param view
  33. */
  34. public void onClick3(View view) {
  35. requestPermission(new String[]{Manifest.permission.CAMERA}, 0x0003);
  36. }
  37. /**
  38. * 权限成功回调函数
  39. *
  40. * @param requestCode
  41. */
  42. @Override
  43. public void permissionSuccess(int requestCode) {
  44. super.permissionSuccess(requestCode);
  45. switch (requestCode) {
  46. case 0x0001:
  47. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:13468857714"));
  48. startActivity(intent);
  49. break;
  50. }
  51. }
  52. }


轻量级封装原链接:http://blog.csdn.net/linglongxin24/article/details/53189359


文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yechaoa/article/details/61920584

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。