6.0之前是install time permissions model,6.0之后取是runtime permissions model。
后者在安装的时候会跟用户显示所需要的权限,并在app运行的时候去申请权限。
Android将系统权限分成了四个保护等级normal,dangerous,signature,signatureOrSystem。
最常见的是
normal permission和
dangerous permission两类。
Normal permission:这类权限可以在app安装期间被默认赋予
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 permission
:这类权限必须在运行的时候申请
CALENDAR:READ_CALENDAR;WRITE_CALENDAR
CAMERA:CAMERA
CONTACTS:READ_CONTACTS;WRITE_CONTACTS;GET_ACCOUNTS
LOCATION:ACCESS_FINE_LOCATION;ACCESS_COARSE_LOCATION
MICROPHONE:RECORD_AUDIO
PHONE:READ_PHONE_STATE;CALL_PHONE;READ_CALL_LOG;WRITE_CALL_LOG;ADD_VOICEMAIL;USE_SIP;PROCESS_OUTGOING_CALLS
SENSORS:BODY_SENSORS
SMS:SEND_SMS;RECEIVE_SMS;READ_SMS;RECEIVE_WAP_PUSH;RECEIVE_MMS
STORAGE:READ_EXTERNAL_STORAGE;WRITE_EXTERNAL_STORAGE
API23检查和请求权限的方法是:
checkSelfPermission和
requestPermissions(API23)
流程是:先检查有没有权限,如果有直接调用,如果没有,则请求权限,接下来
论用户同意还是拒绝,activity的onRequestPermissionsResult会被回调来通知结果(通过第三个参数)。
如果用户第一次拒绝权限,下次
requestPermissions的时候使用shouldShowRequestPermissionRationale来跟用户解释。
使用方法(以使用camera权限为例):
1、按照github上配置build.gradle
2、使用的Activity或Fragment添加注解
@RuntimePermissions
3、在打开camera的click事件里面添加:
MainActivityPermissionsDispatcher.showCameraWithCheck(MainActivity.this);
4、添加注解
@NeedsPermissionm
这里的方法是调用camera的方法;
添加注解
@OnShowRationale
这里的方法是拒绝权限再想调用会弹出的为什么需要这个权限;
添加注解
@OnPermissionDenied
这里的方法是拒绝权限之后再调用弹出;
添加注解
@OnNeverAskAgain
这里的方法是用户勾选不在询问执行的;
参考:
android 6.0权限:
http://www.cnblogs.com/zqlxtt/p/4873831.html
泡网上的解释:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0830/3387.html
Demo:AndroidMPermissionTest