首页 > 学院 > 开发设计 > 正文

安卓6.0权限使用

2019-11-06 09:37:37
字体:
来源:转载
供稿:网友

很久没写过博客了,今天有空写一下.

安卓6.0的权限机制分为两类

Normal Permissions.

android.permission.access_LOCATION_EXTRA_COMMANDS android.permission.ACCESS_NETWORK_STATE android.permission.ACCESS_NOTIFICATION_POLICY android.permission.ACCESS_WIFI_STATE android.permission.ACCESS_WIMAX_STATE android.permission.BLUETOOTH android.permission.BLUETOOTH_ADMIN android.permission.BROADCAST_STICKY android.permission.CHANGE_NETWORK_STATE android.permission.CHANGE_WIFI_MULTICAST_STATE android.permission.CHANGE_WIFI_STATE android.permission.CHANGE_WIMAX_STATE android.permission.DISABLE_KEYGUARD android.permission.EXPAND_STATUS_BAR android.permission.FlashLIGHT android.permission.GET_ACCOUNTS android.permission.GET_PACKAGE_SIZE android.permission.INTERNET android.permission.KILL_BACKGROUND_PROCESSES android.permission.MODIFY_AUDIO_SETTINGS android.permission.NFC android.permission.READ_SYNC_SETTINGS android.permission.READ_SYNC_STATS android.permission.RECEIVE_BOOT_COMPLETED android.permission.REORDER_TASKS android.permission.REQUEST_INSTALL_PACKAGES android.permission.SET_TIME_ZONE android.permission.SET_WALLPAPER android.permission.SET_WALLPAPER_HINTS android.permission.SUBSCRIBED_FEEDS_READ android.permission.TRANSMIT_IR android.permission.USE_FINGERPRINT android.permission.VIBRATE android.permission.WAKE_LOCK android.permission.WRITE_SYNC_SETTINGS com.android.alarm.permission.SET_ALARM com.android.launcher.permission.INSTALL_SHORTCUT com.android.launcher.permission.UNINSTALL_SHORTCUT

Dangerous Permissions

group:android.permission-group.CONTACTS permission:android.permission.WRITE_CONTACTS permission:android.permission.GET_ACCOUNTS permission:android.permission.READ_CONTACTS group: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_VOICEMAIL group:android.permission-group.CALENDAR permission:android.permission.READ_CALENDAR permission:android.permission.WRITE_CALENDAR group:android.permission-group.CAMERA permission:android.permission.CAMERA group:android.permission-group.SENSORS permission:android.permission.BODY_SENSORS group:android.permission-group.LOCATION permission:android.permission.ACCESS_FINE_LOCATION permission:android.permission.ACCESS_COARSE_LOCATION group:android.permission-group.STORAGE permission:android.permission.READ_EXTERNAL_STORAGE permission:android.permission.WRITE_EXTERNAL_STORAGE group:android.permission-group.MICROPHONE permission:android.permission.RECORD_AUDIO group: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

一、自己申请权限

1.在AndroidManifest里面配置需要的权限,无论是普通的还是特殊的权限都需要在这里配置。 2.检查权限,如果没有权限则申请 3.给用户申请权限的解释,这个方法只会在第一次用户拒绝授权,再次去申请这个的权限时会用到 4.申请相应的权限 5.处理申请权限的回调,在这里获得哪些授权成功哪些失败,处理需要的逻辑

二、使用第三方开源库

EasyPermissions

compile 'pub.devrel:easypermissions:0.1.5' public class BaseActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{ //--------------------------------------权限封装------------------------------------------- private Map<Integer, PermissionCallback> mPermissonCallbacks; /** * 权限回调接口 */ protected interface PermissionCallback { /** * 成功获取权限 */ void haspermission(); } /** * 请求权限操作 * @param rationale 请求权限提示语 * @param permissionRequestCode 权限requestCode * @param perms 申请的权限列表 * @param callback 权限结果回调 */ protected void performCodeWithPermission(@NonNull String rationale, final int permissionRequestCode, @NonNull String[] perms, @NonNull PermissionCallback callback) { if (EasyPermissions.hasPermissions(this, perms)) { callback.hasPermission(); } else { if(mPermissonCallbacks == null){ mPermissonCallbacks = new HashMap<>(); } mPermissonCallbacks.put(permissionRequestCode, callback); EasyPermissions.requestPermissions(this, rationale, permissionRequestCode, perms); } } /** * 跳转设置弹框 建议在权限被设置为不在询问时弹出 提示用户前往设置页面打开权限 * @param tips 提示信息 */ protected void alertAppSetPermission(String tips) { new AppSettingsDialog.Builder(this, tips) .setTitle(getString(R.string.permission_deny_again_title)) .setPositiveButton(getString(R.string.permission_deny_again_positive)) .setNegativeButton(getString(R.string.permission_deny_again_nagative), null) .build() .show(); } /** * 跳转设置弹框 建议在权限被设置为不在询问时弹出 提示用户前往设置页面打开权限 * @param tips 提示信息 * @param requestCode 页面返回时onActivityResult的requestCode */ protected void alertAppSetPermission(String tips, int requestCode) { new AppSettingsDialog.Builder(this, tips) .setTitle(getString(R.string.permission_deny_again_title)) .setPositiveButton(getString(R.string.permission_deny_again_positive)) .setNegativeButton(getString(R.string.permission_deny_again_nagative), null) .setRequestCode(requestCode) .build() .show(); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //这里会回调onPermissionsGranted 和 onPermissionsDenied EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } @Override public void onPermissionsGranted(int requestCode, List<String> perms) { PermissionCallback callback = mPermissonCallbacks.get(requestCode); if(callback != null) { callback.hasPermission(); } } @Override public void onPermissionsDenied(int requestCode, List<String> perms) { PermissionCallback callback = mPermissonCallbacks.get(requestCode); if(callback != null) { if(EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { alertAppSetPermission(getString(R.string.permission_tips), REQUEST_APPSET); } else { //普通取消(不授权也非不再询问) ,不做任何提示 } } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表