android6.0之后危险类的权限是需要申请用户确认后才能使用该权限的,下面写出权限申请的工具类,可申请一个权限或同时申请多个权限,可以是在Activity调用也可在fragment中调用。
代码如下:CheckPermission
package com.antbyte.video.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
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.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import com.antbyte.video.MyApp;
import com.antbyte.video.R;
import com.antbyte.video.activities.MainActivity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yangjianli on 2016/4/5 0005.
*/
public class CheckPermission {
private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
/**
* 6.0以上:activity单个权限申请
* @param context
* @param permissionName
* @param requstCode
* @return
*/
public static boolean requestPermission(Activity context, String permissionName, int requstCode) {
if (ContextCompat.checkSelfPermission(context, permissionName) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, new String[]{permissionName}, requstCode);
return false;
} else {
return true;
}
}
/**
* 6.0以上:activity多个权限申请
* @param context
* @param permissions
* @param requstCode
* @return
*/
public static boolean requestPermissions(Activity context, List<String> permissions, int requstCode){
List<String> needRequetPermissions = new ArrayList<>();
for(int i = 0;i < permissions.size();i++){
String permission = permissions.get(i);
if(ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED){
needRequetPermissions.add(permission);
}
}
int requestPermissionCount = needRequetPermissions.size();
if(requestPermissionCount > 0){
String[]requestPermissions = new String[requestPermissionCount];
for(int i = 0;i < requestPermissionCount;i++){
requestPermissions[i] = needRequetPermissions.get(i);
}
ActivityCompat.requestPermissions(context, requestPermissions, requstCode);
return false;
}else{
return true;
}
}
/**
*:6.0以上:fragment的单个权限申请
* @param activity
* @param fragment
* @param permissionName
* @param requstCode
* @return
*/
@TargetApi(Build.VERSION_CODES.M)
public static boolean requestPermissionFragment(Activity activity, Fragment fragment, String permissionName, int requstCode) {
if (ContextCompat.checkSelfPermission(activity, permissionName) != PackageManager.PERMISSION_GRANTED) {
fragment.requestPermissions(new String[]{permissionName}, requstCode);
return false;
} else {
return true;
}
}
/**
*:6.0以上:fragment的多个权限申请
* @param activity
* @param fragment
* @param permissions
* @param requstCode
* @return
*/
@TargetApi(Build.VERSION_CODES.M)
public static boolean requestPermissionFragments(Activity activity, Fragment fragment, List<String> permissions, int requstCode) {
List<String> needRequetPermissions = new ArrayList<>();
for(int i = 0;i < permissions.size();i++){
String permission = permissions.get(i);
if(ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED){
needRequetPermissions.add(permission);
}
}
int requestPermissionCount = needRequetPermissions.size();
if(requestPermissionCount > 0){
String[]requestPermissions = new String[requestPermissionCount];
for(int i = 0;i < requestPermissionCount;i++){
requestPermissions[i] = needRequetPermissions.get(i);
}
fragment.requestPermissions(requestPermissions, requstCode);
return false;
}else{
return true;
}
}
// 显示缺失权限提示
public static void showMissingPermissionDialog(final Activity context, String permission) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getResources().getString(R.string.dialog_permission_title));
builder.setMessage("由于"+ MyApp.mApp.getResources().getString(R.string.app_name)+"不能获取设备信息的权限,不能正常运行,请设置" + permission + "权限后使用。设置路径:系统设置->"+ MyApp.mApp.getResources().getString(R.string.app_name)+"->权限");
builder.setCancelable(false);
// 拒绝, 退出应用
builder.setNegativeButton(context.getResources().getString(R.string.dialog_permission_negative), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("EXIT_TAG", "SINGLETASK");
context.startActivity(intent);
}
});
builder.setPositiveButton(context.getResources().getString(R.string.dialog_permission_positive), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
goSetting(context);
}
});
builder.show();
}
//去应用手动设置权限
public static void goSetting(Activity context){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_URL_SCHEME + context.getPackageName()));
context.startActivity(intent);
}
}
最后入过用户不同意则提示用户去设置中打开权限。
申请的代码示例:
private void goCapture() {
List<String> strings = new ArrayList<>();
strings.add(Manifest.permission.READ_EXTERNAL_STORAGE);
strings.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
strings.add(Manifest.permission.CAMERA);
if (CheckPermission.requestPermissions(this, strings, 1)) {
//已经拥有权限后执行的操作
}
}
权限申请的回调如下:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
LogUtil.e("requestCode", "requestCode" + requestCode);
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//用户同意了权限请求后的操作
} else {
CheckPermission.showMissingPermissionDialog(CreateVideoActivity.this, "相机");
}
}
break;
}
}
|