文档章节

android6.0申请权限工具类

moz1q1
 moz1q1
发布于 2016/11/08 12:17
字数 609
阅读 98
收藏 0

简单整理,可能不怎么好

public class PermissionUtils {

	public final static String[] ContactsPermissions = new String[] { Manifest.permission.WRITE_CONTACTS,
			Manifest.permission.GET_ACCOUNTS, Manifest.permission.READ_CONTACTS };

	/**
	 * 通讯录权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkContactsPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestContactsPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, ContactsPermissions, requestCode);
	}

	public final static String[] PhonePermissions = new String[] { Manifest.permission.READ_CALL_LOG,
			Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_CALL_LOG,
			Manifest.permission.USE_SIP, Manifest.permission.PROCESS_OUTGOING_CALLS,
			Manifest.permission.ADD_VOICEMAIL };

	/**
	 * 通话权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkPhonePermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.USE_SIP) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.ADD_VOICEMAIL) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestPhonePermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, PhonePermissions, requestCode);
	}

	public final static String[] CalendarPermissions = new String[] { Manifest.permission.READ_CALENDAR,
			Manifest.permission.WRITE_CALENDAR };

	/**
	 * 日历权限
	 * 
	 * @param context
	 * @param requestCode
	 * @return
	 */
	public static boolean checkCalendarPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestCalendarPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, CalendarPermissions, requestCode);
	}

	public final static String[] CameraPermissions = new String[] { Manifest.permission.CAMERA };

	/**
	 * 相机权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkCameraPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestCameraPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, CameraPermissions, requestCode);
	}

	public final static String[] BodySensorsPermissions = new String[] { Manifest.permission.BODY_SENSORS };

	/**
	 * 身体传感器权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkBodySensorsPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.BODY_SENSORS) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestBodySensorsPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, BodySensorsPermissions, requestCode);
	}

	public final static String[] LocationPermissions = new String[] { Manifest.permission.ACCESS_FINE_LOCATION,
			Manifest.permission.ACCESS_COARSE_LOCATION };

	/**
	 * 定位权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkLocationPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestLocationPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, LocationPermissions, requestCode);
	}

	public final static String[] MicrophonePermissions = new String[] { Manifest.permission.RECORD_AUDIO };

	/**
	 * 录音权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkMicrophonePermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestMicrophonePermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, MicrophonePermissions, requestCode);
	}

	public final static String[] SmsPermissions = new String[] { Manifest.permission.READ_SMS,
			Manifest.permission.RECEIVE_WAP_PUSH, Manifest.permission.RECEIVE_MMS, Manifest.permission.RECEIVE_SMS,
			Manifest.permission.SEND_SMS };

	/**
	 * 短信权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkSmsPermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.RECEIVE_WAP_PUSH) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.RECEIVE_MMS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestSmsPermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, SmsPermissions, requestCode);
	}

	public final static String[] StoragePermissions = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,
			Manifest.permission.WRITE_EXTERNAL_STORAGE };

	/**
	 * 存储权限
	 * 
	 * @param context
	 * @return
	 */
	public static boolean checkStoragePermissions(Context context) {
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {// 没有权限
			return false;
		} else {
			return true;
		}
	}

	public static void requestStoragePermissions(Activity context, int requestCode) {
		ActivityCompat.requestPermissions(context, StoragePermissions, requestCode);
	}

	/**
	 * http://www.cnblogs.com/fangyucun/p/4027750.html
	 * 
	 * @param context
	 */
	public static void getAppDetailSettingIntent(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());
		}
		if (isIntentAvailable(context, localIntent)) {
			context.startActivity(localIntent);
		}
	}

	/**
	 * 判断是否有可以接受的Activity
	 * 
	 * @param context
	 * @param action
	 * @return
	 */
	public static boolean isIntentAvailable(Context context, Intent intent) {
		if (intent == null)
			return false;
		return context.getPackageManager().queryIntentActivities(intent, PackageManager.GET_ACTIVITIES).size() > 0;
	}

}

 

© 著作权归作者所有

共有 人打赏支持
moz1q1
粉丝 55
博文 351
码字总数 117569
作品 0
东莞
程序员
私信 提问
Vanish/RTPermission

RTPermission Android6.0 运行时权限申请工具,Android Marshmallow runtime permission utils RTPermission 想了解该项目可参考下面的博客: http://www.jianshu.com/p/1841a74543c1 http:/......

Vanish
2017/05/22
0
0
android开发之调皮的权限

文章背景 撰写本文的目的在于帮助开发者们解决android中动态申请权限的坑。 在android6.0以前,申请权限的方式很简单,只需要在清单文件AndroidManifest.xml中申请即可。需要用到时系统会弹框...

安卓小哥
2017/04/22
0
0
Android permission 工具类

一行代码搞定Android6.0动态权限授权、权限管理 引入 compile 'com.github.dfqin:grantor:2.5' https://github.com/dfqin/PermissionGrantor 声明权限 再oncreate 申请...

Jay_kyzg
05/25
0
0
小明/EasyPermissionsActivity

EasyPermissionsActivity 项目介绍 超级简单的 Android6.0动态权限申请程序 Github地址:稍后更新 Gitee 地址:https://gitee.com/hmOS/EasyPermissionsActivity 使用说明 EasyPermissionsA...

小明
10/23
0
0
android权限问题

虽然现在android已经出了7.0了。但是大部分人用的应该还是5.0和6.0的。 其中对于开发者来说,变化比较大的应该是6.0之前和6.0之后的版本。 因为以6.0为分界线多了一个比较坑的东西:权限的动...

nesger
2016/12/02
149
0

没有更多内容

加载失败,请刷新页面

加载更多

PHP生成CSV之内部换行

当我们使用PHP将采集到的文件内容保存到csv文件时,往往需要将采集内容进行二次过滤处理才能得到需要的内容。比如网页中的换行符,空格符等等。 对于空格等处理起来都比较简单,这里我们单独...

豆花饭烧土豆
13分钟前
0
0
使用 mjml 生成 thymeleaf 邮件框架模板

发邮件算是系统开发的一个基本需求了,不过搞邮件模板实在是件恶心事,估计搞过的同仁都有体会。 得支持多种客户端 支持响应式 疼彻心扉的 outlook 多数客户端只支持 inline 形式的 css 布局...

郁也风
16分钟前
2
0
让哲学照亮我们的人生——读《医务工作者需要学点哲学》有感2600字

让哲学照亮我们的人生——读《医务工作者需要学点哲学》有感2600字: 作者:孙冬梅;以前读韩国前总统朴槿惠的著作《绝望锻炼了我》时,里面有一句话令我印象深刻,她说“在我最困难的时期,...

原创小博客
今天
3
0
JAVA-四元数类

public class Quaternion { private final double x0, x1, x2, x3; // 四元数构造函数 public Quaternion(double x0, double x1, double x2, double x3) { this.x0 = ......

Pulsar-V
今天
17
0
Xshell利用Xftp传输文件,使用pure-ftpd搭建ftp服务

Xftp传输文件 如果已经通过Xshell登录到服务器,此时可以使用快捷键ctrl+alt+f 打开Xftp并展示Xshell当前的目录,之后直接拖拽传输文件即可。 pure-ftpd搭建ftp服务 pure-ftpd要比vsftp简单,...

野雪球
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部