文档章节

android6.0申请权限工具类

moz1q1
 moz1q1
发布于 2016/11/08 12:17
字数 609
阅读 96
收藏 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
粉丝 53
博文 349
码字总数 117339
作品 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
android权限问题

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

nesger
2016/12/02
134
0
Android6.0动态权限管理库

在Android开发中,申请个权限本来很简单的事情,可就因为Android6.0出个动态权限,整的那个复杂度是几何倍增。本着我们开发向简的宗旨,今天我在这推荐一个Android6.0动态权限管理库Runtime...

wkp111
2017/12/27
0
0

没有更多内容

加载失败,请刷新页面

加载更多

你为什么在Redis里读到了本应过期的数据

一个事故的故事 晚上睡的正香突然被电话吵醒,对面是开发焦急的声音:我们的程序在访问redis的时候读到了本应过期的key导致整个业务逻辑出了问题,需要马上解决。 看到这里你可能会想:这是不...

IT--小哥
今天
2
0
祝大家节日快乐,阖家幸福! centos GnuTLS 漏洞

yum update -y gnutls 修复了GnuTLS 漏洞。更新到最新 gnutls.x86_64 0:2.12.23-22.el6 版本

yizhichao
昨天
5
0
Scrapy 1.5.0之选择器

构造选择器 Scrapy选择器是通过文本(Text)或 TextResponse 对象构造的 Selector 类的实例。 它根据输入类型自动选择最佳的解析规则(XML vs HTML): >>> from scrapy.selector import Sele...

Eappo_Geng
昨天
4
0
Windows下Git多账号配置,同一电脑多个ssh-key的管理

Windows下Git多账号配置,同一电脑多个ssh-key的管理   这一篇文章是对上一篇文章《Git-TortoiseGit完整配置流程》的拓展,所以需要对上一篇文章有所了解,当然直接往下看也可以,其中也有...

morpheusWB
昨天
5
0
中秋快乐!!!

HiBlock
昨天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部