Android6.0运行时权限。
【摘要】
一、集成三方库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'
-
compile 'com.github.hotchemi:permissionsdispatcher:2.2.0'
-
apt 'com.github.hotchemi:permissionsdispatcher-processor:2.2.0'
2.给Activity或者Fragment注册
-
@RuntimePermissions
-
public class LoginActivity extends AppCompatActivity{
3.实现五个方法
-
/**
-
* 处理当用户允许该权限时需要处理的方法(括号里面是当前需要请求的权限)
-
*/
-
@NeedsPermission(Manifest.permission.READ_PHONE_STATE)
-
void doACacheNeedsPermission() {
-
//do something
-
}
-
@OnShowRationale(Manifest.permission.READ_PHONE_STATE)
-
void ACacheShowRationale(PermissionRequest request) {
-
request.proceed(); // 提示用户权限使用的对话框
-
}
-
-
@OnNeverAskAgain(Manifest.permission.READ_PHONE_STATE)
-
void ACacheOnNeverAskAgain() {
-
new AlertDialog.Builder(this)
-
.setPositiveButton("重新设置", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(@NonNull DialogInterface dialog, int which) {
-
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
-
intent.setData(Uri.parse("package:" + getPackageName()));
-
startActivity(intent);
-
}
-
})
-
.setNegativeButton("取消", null)
-
.setCancelable(false)
-
.setMessage("您已拒绝授予权限")
-
.show();
-
}
-
-
/**
-
* 如果用户拒绝该权限执行的方法
-
*/
-
@OnPermissionDenied(Manifest.permission.READ_PHONE_STATE)
-
void ACacheOnPermissionDenied() {
-
MyUtil.showToast("真的确定拒绝授予权限吗");
-
}
-
/**
-
* 权限请求回调,提示用户之后,用户点击“允许”或者“拒绝”之后调用此方法
-
* @param requestCode 定义的权限编码
-
* @param permissions 权限名称
-
* @param grantResults 允许/拒绝
-
*/
-
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
-
LoginActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
-
}
比如是一个点击事件
-
case R.id.btn:
-
//6.0权限
-
LoginActivityPermissionsDispatcher.doACacheNeedsPermissionWithCheck(this);
-
break;
二、Android6.0M权限管理实战,完美轻量级封装
1.第一步:检测所有的权限是否都已授权
-
/**
-
* 检测所有的权限是否都已授权
-
*
-
* @param permissions
-
* @return
-
*/
-
private boolean checkPermissions(String[] permissions) {
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
-
return true;
-
}
-
-
for (String permission : permissions) {
-
if (ContextCompat.checkSelfPermission(this, permission) !=
-
PackageManager.PERMISSION_GRANTED) {
-
return false;
-
}
-
}
-
return true;
-
}
2.第二步:获取权限集中需要申请权限的列表
-
/**
-
* 获取权限集中需要申请权限的列表
-
*
-
* @param permissions
-
* @return
-
*/
-
private List<String> getDeniedPermissions(String[] permissions) {
-
List<String> needRequestPermissionList = new ArrayList<>();
-
for (String permission : permissions) {
-
if (ContextCompat.checkSelfPermission(this, permission) !=
-
PackageManager.PERMISSION_GRANTED ||
-
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
-
needRequestPermissionList.add(permission);
-
}
-
}
-
return needRequestPermissionList;
-
}
-
/**
-
* 请求权限
-
*
-
* @param permissions 请求的权限
-
* @param requestCode 请求权限的请求码
-
*/
-
public void requestPermission(String[] permissions, int requestCode) {
-
this.REQUEST_CODE_PERMISSION = requestCode;
-
if (checkPermissions(permissions)) {
-
permissionSuccess(REQUEST_CODE_PERMISSION);
-
} else {
-
List<String> needPermissions = getDeniedPermissions(permissions);
-
ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
-
}
-
}
-
/**
-
* 系统请求权限回调
-
*
-
* @param requestCode
-
* @param permissions
-
* @param grantResults
-
*/
-
@Override
-
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
-
if (requestCode == REQUEST_CODE_PERMISSION) {
-
if (verifyPermissions(grantResults)) {
-
permissionSuccess(REQUEST_CODE_PERMISSION);
-
} else {
-
permissionFail(REQUEST_CODE_PERMISSION);
-
showTipsDialog();
-
}
-
}
-
}
-
/**
-
* 确认所有的权限是否都已授权
-
*
-
* @param grantResults
-
* @return
-
*/
-
private boolean verifyPermissions(int[] grantResults) {
-
for (int grantResult : grantResults) {
-
if (grantResult != PackageManager.PERMISSION_GRANTED) {
-
return false;
-
}
-
}
-
return true;
-
}
-
/**
-
* 获取权限成功
-
*
-
* @param requestCode
-
*/
-
public void permissionSuccess(int requestCode) {
-
Log.d(TAG, "获取权限成功=" + requestCode);
-
-
}
-
/**
-
* 权限获取失败
-
* @param requestCode
-
*/
-
public void permissionFail(int requestCode) {
-
Log.d(TAG, "获取权限失败=" + requestCode);
-
}
-
-
/**
-
* 显示提示对话框
-
*/
-
private void showTipsDialog() {
-
new AlertDialog.Builder(this)
-
.setTitle("提示信息")
-
.setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
-
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
}
-
})
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
startAppSettings();
-
}
-
}).show();
-
}
-
/**
-
* 启动当前应用设置页面
-
*/
-
private void startAppSettings() {
-
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
-
intent.setData(Uri.parse("package:" + getPackageName()));
-
startActivity(intent);
-
}
-
package cn.bluemobi.dylan.mpermissions;
-
-
import android.content.DialogInterface;
-
import android.content.Intent;
-
import android.content.pm.PackageManager;
-
import android.net.Uri;
-
import android.os.Build;
-
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.util.Log;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
-
/**
-
* Created by yuandl on 2016-11-16.
-
*/
-
-
public class MPermissionsActivity extends AppCompatActivity {
-
private final String TAG = "MPermissions";
-
private int REQUEST_CODE_PERMISSION = 0x00099;
-
-
/**
-
* 请求权限
-
*
-
* @param permissions 请求的权限
-
* @param requestCode 请求权限的请求码
-
*/
-
public void requestPermission(String[] permissions, int requestCode) {
-
this.REQUEST_CODE_PERMISSION = requestCode;
-
if (checkPermissions(permissions)) {
-
permissionSuccess(REQUEST_CODE_PERMISSION);
-
} else {
-
List<String> needPermissions = getDeniedPermissions(permissions);
-
ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
-
}
-
}
-
-
/**
-
* 检测所有的权限是否都已授权
-
*
-
* @param permissions
-
* @return
-
*/
-
private boolean checkPermissions(String[] permissions) {
-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
-
return true;
-
}
-
-
for (String permission : permissions) {
-
if (ContextCompat.checkSelfPermission(this, permission) !=
-
PackageManager.PERMISSION_GRANTED) {
-
return false;
-
}
-
}
-
return true;
-
}
-
-
/**
-
* 获取权限集中需要申请权限的列表
-
*
-
* @param permissions
-
* @return
-
*/
-
private List<String> getDeniedPermissions(String[] permissions) {
-
List<String> needRequestPermissionList = new ArrayList<>();
-
for (String permission : permissions) {
-
if (ContextCompat.checkSelfPermission(this, permission) !=
-
PackageManager.PERMISSION_GRANTED ||
-
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
-
needRequestPermissionList.add(permission);
-
}
-
}
-
return needRequestPermissionList;
-
}
-
-
-
/**
-
* 系统请求权限回调
-
*
-
* @param requestCode
-
* @param permissions
-
* @param grantResults
-
*/
-
@Override
-
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
-
if (requestCode == REQUEST_CODE_PERMISSION) {
-
if (verifyPermissions(grantResults)) {
-
permissionSuccess(REQUEST_CODE_PERMISSION);
-
} else {
-
permissionFail(REQUEST_CODE_PERMISSION);
-
showTipsDialog();
-
}
-
}
-
}
-
-
/**
-
* 确认所有的权限是否都已授权
-
*
-
* @param grantResults
-
* @return
-
*/
-
private boolean verifyPermissions(int[] grantResults) {
-
for (int grantResult : grantResults) {
-
if (grantResult != PackageManager.PERMISSION_GRANTED) {
-
return false;
-
}
-
}
-
return true;
-
}
-
-
/**
-
* 显示提示对话框
-
*/
-
private void showTipsDialog() {
-
new AlertDialog.Builder(this)
-
.setTitle("提示信息")
-
.setMessage("当前应用缺少必要权限,该功能暂时无法使用。如若需要,请单击【确定】按钮前往设置中心进行权限授权。")
-
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
}
-
})
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
startAppSettings();
-
}
-
}).show();
-
}
-
-
/**
-
* 启动当前应用设置页面
-
*/
-
private void startAppSettings() {
-
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
-
intent.setData(Uri.parse("package:" + getPackageName()));
-
startActivity(intent);
-
}
-
-
/**
-
* 获取权限成功
-
*
-
* @param requestCode
-
*/
-
public void permissionSuccess(int requestCode) {
-
Log.d(TAG, "获取权限成功=" + requestCode);
-
-
}
-
-
/**
-
* 权限获取失败
-
* @param requestCode
-
*/
-
public void permissionFail(int requestCode) {
-
Log.d(TAG, "获取权限失败=" + requestCode);
-
}
-
}
1.布局文件activity_main.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:id="@+id/activity_main"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
android:paddingBottom="@dimen/activity_vertical_margin"
-
android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
tools:context="cn.bluemobi.dylan.mpermissions.MainActivity">
-
-
<TextView
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="Hello World!" />
-
-
<Button
-
android:id="@+id/button"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:onClick="onClick1"
-
android:text="打电话" />
-
-
<Button
-
android:id="@+id/button2"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:onClick="onClick2"
-
android:text="写SD卡" />
-
-
<Button
-
android:id="@+id/button3"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:onClick="onClick3"
-
android:text="拍照" />
-
</LinearLayout>
-
package cn.bluemobi.dylan.mpermissions;
-
-
import android.Manifest;
-
import android.content.Intent;
-
import android.net.Uri;
-
import android.os.Bundle;
-
import android.view.View;
-
-
public class MainActivity extends MPermissionsActivity {
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
}
-
-
/**
-
* 打电话
-
*
-
* @param view
-
*/
-
public void onClick1(View view) {
-
requestPermission(new String[]{Manifest.permission.CALL_PHONE}, 0x0001);
-
}
-
-
/**
-
* 写SD卡
-
*
-
* @param view
-
*/
-
public void onClick2(View view) {
-
requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0x0002);
-
}
-
-
/**
-
* 拍照
-
*
-
* @param view
-
*/
-
public void onClick3(View view) {
-
requestPermission(new String[]{Manifest.permission.CAMERA}, 0x0003);
-
}
-
-
/**
-
* 权限成功回调函数
-
*
-
* @param requestCode
-
*/
-
@Override
-
public void permissionSuccess(int requestCode) {
-
super.permissionSuccess(requestCode);
-
switch (requestCode) {
-
case 0x0001:
-
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:13468857714"));
-
startActivity(intent);
-
break;
-
}
-
-
}
-
-
}
轻量级封装原链接:http://blog.csdn.net/linglongxin24/article/details/53189359
文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/yechaoa/article/details/61920584
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)