android 动态获取权限有哪些,Android 6.0+ 动态获取权限

Android 6.0+ 动态获取权限

这里有一个现成的库,可以直接拿来用。方便简单

1.向app下的gradle添加依赖:

dependencies{

// android 6.0+ 动态获取权限

compile'com.master.android:permissionhelper:1.3'

}

2.在你的app启动的第一个activity中进行初始化

private static final StringTAG ="Sessin -->>> 6.0+权限申请";

private PermissionHelperpermissionHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// android 6.0+ 动态获取权限

permissionHelper =new PermissionHelper(this,new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},100);

permissionHelper.request(this);

}

3.实现相关方法

@Override

public void onPermissionGranted() {

//全都授权

Log.i(TAG,"已授权...");

}

@Override

public void onIndividualPermissionGranted(String[] grantedPermission) {

//某个授权

Log.i(TAG,"onIndividualPermissionGranted() called with: grantedPermission = [" + TextUtils.join(",", grantedPermission) +"]");

}

@Override

public void onPermissionDenied() {

//某个拒绝

Log.w(TAG,"onPermissionDenied...");

}

@Override

public void onPermissionDeniedBySystem() {

//用户选择了"不再询问"后,点击"拒绝按钮",执行此方法

Log.w(TAG,"onPermissionDeniedBySystem...");

}

4.如果app有一些必须申请的权限,不申请会影响app正常使用的。比如:定位、相机、读取存储器等

@Override

public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (permissionHelper !=null) {

permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);

}

if (!PermissionUtil.verifyPermissions(grantResults)) {

openPermissionSetting();

}

}

这是授权结果的回调。如果用户拒绝了权限执行openPermissionSetting();方法,让用户去手机设置中对权限进行手动添加,如果取消,app则会退出。

以上就是权限动态获取的全过程。

在贴一个PermissionUtil工具类和openPermissionSetting();方法的代码

PermissionUtil:

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Build;

/**

* Utility class that wraps access to the runtime permissions API in M and provides basic helper

* methods.

*/

public abstract class PermissionUtil {

/**

* Check that all given permissions have been granted by verifying that each entry in the

* given array is of the value {@link PackageManager#PERMISSION_GRANTED}.

*

* @see Activity#onRequestPermissionsResult(int, String[], int[])

*/

public static boolean verifyPermissions(int[] grantResults) {

// At least one result must be checked.

if(grantResults.length <1){

return false;

}

// Verify that each required permission has been granted, otherwise return false.

for (int result : grantResults) {

if (result != PackageManager.PERMISSION_GRANTED) {

return false;

}

}

return true;

}

public static void showPermisionSetting(Context context){

Intent localIntent =new Intent();

localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (Build.VERSION.SDK_INT >=9) {

localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");

localIntent.setData(Uri.fromParts("package", context.getPackageName(),null));

}else if (Build.VERSION.SDK_INT <=8) {

localIntent.setAction(Intent.ACTION_VIEW);

localIntent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");

localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());

}

context.startActivity(localIntent);

}

}

openPermissionSetting();

/**

* 打开 APP 的详情设置

*/

private void openPermissionSetting() {

AlertDialog.Builder builder =new AlertDialog.Builder(this,

R.style.Theme_AppCompat_Light_Dialog);

builder.setMessage(R.string.permission_lead_tips);

builder

.setPositiveButton(R.string.go_hand_permission,new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

Intent intent =new Intent();

intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

intent.addCategory(Intent.CATEGORY_DEFAULT);

intent.setData(Uri.parse("package:" + getPackageName()));

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

startActivity(intent);

}

});

builder.setNegativeButton(getString(R.string.cancel),new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,int which) {

finish();

Log.w(TAG,"用户拒绝程序必要权限,因此退出程序!");

}

});

builder.show();

}

希望可以帮助到需要用的人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值