在Adroid系統6.0之前,權限的處理是在App安裝時受權,受權完了才能完成相關的安裝。而在6.0的系統上,是先安裝App,在安裝完以後,在使用相關權限的操做時,纔會彈出權限的提示框,用戶贊成受權以後才能正常使用。谷歌這樣作,可讓用戶更加清醒的認識相關權限的使用,在必定程度上更加人性化和保護了用戶的隱私。android
谷歌官方將權限分爲了兩類,一個是正常權限(Normal Permissions),這類權限不涉及用戶隱私,是不須要用戶進行受權的,好比訪問網絡,手機震動等。還有一類是危險權限(Dangerous Permissions),通常是涉及到用戶隱私的,須要用戶進行受權,好比操做SD卡的寫入,相機,錄音等。網絡
ACCESS_LOCATION_EXTRA_COMMANDSide
ACCESS_NETWORK_STATEui
ACCESS_NOTIFICATION_POLICYspa
ACCESS_WIFI_STATEcode
BLUETOOTHorm
BLUETOOTH_ADMIN文檔
BROADCAST_STICKYit
CHANGE_NETWORK_STATEio
CHANGE_WIFI_MULTICAST_STATE
CHANGE_WIFI_STATE
DISABLE_KEYGUARD
EXPAND_STATUS_BAR
GET_PACKAGE_SIZE
INSTALL_SHORTCUT
INTERNET
KILL_BACKGROUND_PROCESSES
MODIFY_AUDIO_SETTINGS
NFC
READ_SYNC_SETTINGS
READ_SYNC_STATS
RECEIVE_BOOT_COMPLETED
REORDER_TASKS
REQUEST_INSTALL_PACKAGES
SET_ALARM
SET_TIME_ZONE
SET_WALLPAPER
SET_WALLPAPER_HINTS
TRANSMIT_IR
UNINSTALL_SHORTCUT
USE_FINGERPRINT
VIBRATE
WAKE_LOCK
WRITE_SYNC_SETTINGS
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
看完官方文檔,還好解決方案還不是太難,也不是很是麻煩,固然和之前相比仍是有一點繁瑣的。廢話很少說了,解決方案以下。
private boolean canMakeSmores(){ return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1); }
檢查系統版本的緣由,顯而易見,若是是系統6.0及以上的,須要採用新的權限受權方法。
舉個例子,若是你須要拍照操做,那麼你就應該在拍照操做的地方,先加上權限申請受權。方式以下:
String[] perms = {"android.permission.CAMERA"}; int permsRequestCode = 200; requestPermissions(perms, permsRequestCode);
@Override public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){ switch(permsRequestCode){ case 200: boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED; if(cameraAccepted){ //受權成功以後,調用系統相機進行拍照操做等 }else{ //用戶受權拒絕以後,友情提示一下就能夠了 } break; } }
經過上面能夠看出,只檢查系統版本了,若是受權過的權限,還須要再次去受權麼?咱們的判斷其實能夠更完善一下,好比:
private boolean hasPermission(String permission){ if(canMakeSmores()){ return(checkSelfPermission(permission)==PackageManager.PERMISSION_GRANTED); } return true; }