本文共 3721 字,大约阅读时间需要 12 分钟。
android 6.0对于权限管理比较收紧,因此在适配android 6.0的时候就很有必要考虑一些权限管理的问题。
如果你没适配6.0的设备并且权限没给的话,就会出现类似如下的问题:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{b83b87a 16850:com.chexiu.sales/u0a34} (pid=16850, uid=10034) with revoked permission android.permission.CALL_PHONE
google官方权限分类:
如下为正常的权限:
ACCESS_LOCATION_EXTRA_COMMANDSACCESS_NETWORK_STATEACCESS_NOTIFICATION_POLICYACCESS_WIFI_STATEBLUETOOTHBLUETOOTH_ADMINBROADCAST_STICKYCHANGE_NETWORK_STATECHANGE_WIFI_MULTICAST_STATECHANGE_WIFI_STATEDISABLE_KEYGUARDEXPAND_STATUS_BARGET_PACKAGE_SIZEINSTALL_SHORTCUTINTERNETKILL_BACKGROUND_PROCESSESMODIFY_AUDIO_SETTINGSNFCREAD_SYNC_SETTINGSREAD_SYNC_STATSRECEIVE_BOOT_COMPLETEDREORDER_TASKSREQUEST_INSTALL_PACKAGESSET_ALARMSET_TIME_ZONESET_WALLPAPERSET_WALLPAPER_HINTSTRANSMIT_IRUNINSTALL_SHORTCUTUSE_FINGERPRINTVIBRATEWAKE_LOCKWRITE_SYNC_SETTINGS
如下为危险权限:
group:android.permission-group.CONTACTS permission:android.permission.WRITE_CONTACTS permission:android.permission.GET_ACCOUNTS permission:android.permission.READ_CONTACTSgroup:android.permission-group.PHONE permission:android.permission.READ_CALL_LOG permission:android.permission.READ_PHONE_STATE permission:android.permission.CALL_PHONE permission:android.permission.WRITE_CALL_LOG permission:android.permission.USE_SIP permission:android.permission.PROCESS_OUTGOING_CALLS permission:com.android.voicemail.permission.ADD_VOICEMAILgroup:android.permission-group.CALENDAR permission:android.permission.READ_CALENDAR permission:android.permission.WRITE_CALENDARgroup:android.permission-group.CAMERA permission:android.permission.CAMERAgroup:android.permission-group.SENSORS permission:android.permission.BODY_SENSORSgroup:android.permission-group.LOCATION permission:android.permission.ACCESS_FINE_LOCATION permission:android.permission.ACCESS_COARSE_LOCATIONgroup:android.permission-group.STORAGE permission:android.permission.READ_EXTERNAL_STORAGE permission:android.permission.WRITE_EXTERNAL_STORAGEgroup:android.permission-group.MICROPHONE permission:android.permission.RECORD_AUDIOgroup:android.permission-group.SMS permission:android.permission.READ_SMS permission:android.permission.RECEIVE_WAP_PUSH permission:android.permission.RECEIVE_MMS permission:android.permission.RECEIVE_SMS permission:android.permission.SEND_SMS permission:android.permission.READ_CELL_BROADCASTS
那么问题来了,因为这些危险权限导致app在6.0上面崩溃就不应该,所以要找好的方法来解决问题:
库的地址:https://github.com/mylhyl/AndroidAcp
项目中的集成和使用:
1,gradle集成:
compile 'com.mylhyl:acp:1.1.5'
2,代码集成进来,方便修改
我采用2的方式
然后看下实际项目中的使用:
比如针对拨打电话权限的6.0适配:
/** * 直接拨打电话 */ public static boolean intentToCall(final Context context, final String phoneNumber) { if (context == null || StringUtil.isEmpty(phoneNumber)) { return false; } //6.0权限处理 Acp.getInstance(context).request(new AcpOptions.Builder().setPermissions( Manifest.permission.CALL_PHONE).build(), new AcpListener() { @Override public void onGranted() { Uri u = Uri.parse("tel:" + phoneNumber); Intent it = new Intent(Intent.ACTION_CALL, u); context.startActivity(it); } @Override public void onDenied(Listpermissions) { } }); return true; }
链式调用,提供授权和授权失败两种情况处理
如果提供权限会调用onGranted(){}方法,你只需要在里面添加授权后的操作就行。
如果授权失败会调用onDenied(){}方法,你可以提醒用户授权失败,并进行后续操作。
并且该库具有如下特点:
M
版本检测当然建议代码集成到项目中来,后期万一修改也比较方便。
转载地址:http://pxsux.baihongyu.com/