android开发6.0权限适配
来源:互联网 发布:java static方法 编辑:程序博客网 时间:2024/06/06 03:16
写博客只是为了方便记忆,希望自己能够坚持下去。
在android开发中,如果项目的targetSdkVersion >= 23,在手机版本为android6.0+的手机上运行时,某些危险的权限需要用户授权,如果用户不同意,而直接运行某些代码,会造成程序的崩溃。
Runtime Permissions
This release introduces a new permissions model, where users can now directly manage app permissions at runtime. This model gives users improved visibility and control over permissions, while streamlining the installation and auto-update processes for app developers. Users can grant or revoke permissions individually for installed apps.
On your apps that target Android 6.0 (API level 23) or higher, make sure to check for and request permissions at runtime. To determine if your app has been granted a permission, call the new checkSelfPermission() method. To request a permission, call the new requestPermissions() method. Even if your app is not targeting Android 6.0 (API level 23), you should test your app under the new permissions model.
For details on supporting the new permissions model in your app, see Working with System Permissions. For tips on how to assess the impact on your app, see Permissions Best Practices.
这是官网的英文介绍。详细https://developer.android.com/training/permissions/best-practices.html#testing
下面写一个小例子来说明一下用法。(获取打电话的权限)
public void onc(View v) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {//用户同意权限 call(); } else {//用户不同意,向用户请求权限 if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { AlertDialog dialog = new AlertDialog.Builder(this) .setMessage("拨打电话需要赋予权限,不打开将无法拨打!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1); } }) .setNegativeButton("取消", null).create(); dialog.show(); return; } ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1); } } public void call() { Intent intent = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse("tel:" + "10086"); intent.setData(data); startActivity(intent); }
这里有3个方法
- checkSelfPermission 判断权限是否授权
(PackageManager.PERMISSION_GRANTED表示已同意,PackageManager.PERMISSION_DENIED未同意) - shouldShowRequestPermissionRationale 如果用户已经拒绝过1次,返回true,其他情况返回false
主要用户给用户提示,当用户拒绝第一次后,提示权限用途,当用户再次拒绝并勾上不再询问后,提示手动打开权限。 - requestPermissions 申请权限
申请权限回调
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { call(); } else { //用户勾选了不再询问 //提示用户手动打开权限 if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) { AlertDialog dialog = new AlertDialog.Builder(this) .setMessage("拨打电话的权限已被禁止,请手动打开") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { 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", MainActivity.this.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", MainActivity.this.getPackageName()); } startActivity(localIntent); } }) .setNegativeButton("取消", null).create(); dialog.show(); } } super.onRequestPermissionsResult(requestCode, permissions, grantResults); }
最后看一下效果
附录–危险权限
Permission Group Permissions
android.permission-group.CALENDAR
android.permission.READ_CALENDAR
android.permission.WRITE_CALENDAR
android.permission-group.CAMERA
android.permission.CAMERA
android.permission-group.CONTACTS
android.permission.READ_CONTACTS
android.permission.WRITE_CONTACTS
android.permission.GET_ACCOUNTS
android.permission-group.LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_COARSE_LOCATION
android.permission-group.MICROPHONE
android.permission.RECORD_AUDIO
android.permission-group.PHONE
android.permission.READ_PHONE_STATE
android.permission.CALL_PHONE
android.permission.READ_CALL_LOG
android.permission.WRITE_CALL_LOG
com.android.voicemail.permission.ADD_VOICEMAIL
android.permission.USE_SIP
android.permission.PROCESS_OUTGOING_CALLS
android.permission-group.SENSORS
android.permission.BODY_SENSORS
android.permission-group.SMS
android.permission.SEND_SMS
android.permission.RECEIVE_SMS
android.permission.READ_SMS
android.permission.RECEIVE_WAP_PUSH
android.permission.RECEIVE_MMS
android.permission.READ_CELL_BROADCASTS
android.permission-group.STORAGE
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
- android开发6.0权限适配
- android开发 权限适配6.0 permissionsdispatcher的安装使用
- Android 6.0权限适配
- Android 6.0权限适配
- android 6.0 权限适配
- Android 6.0开发的权限注意事项
- 关于Android 6.0 + 权限开发 解决办法
- Android开发之6.0动态权限管理
- Android开发6.0权限的封装
- android开发,权限获取
- Android开发权限大全
- Android开发权限大全
- Android开发_android权限
- Android开发权限大全
- android开发 权限大全
- android开发,权限获取
- android开发中的权限
- Android开发中的权限
- Symfony环境搭建
- 通过实例理解AOP
- AndroidStudio上遇到的一些问题(2)
- 汉诺塔
- 关于x++和++x
- android开发6.0权限适配
- [Python]默认函数参数注意点
- AbstractSecurityWebApplicationInitializer
- CentOS 6.x 升级Python2.6.x到Python2.7.x
- 欢迎使用CSDN-markdown编辑器
- 使用JSPatch实现iOS APP动态更新
- iOS开发中常见得运行报错
- View的setTag()和getTag()方法
- jQuery动画与特效