android 6.0對於權限管理比較收緊,所以在適配android 6.0的時候就頗有必要考慮一些權限管理的問題。java
若是你沒適配6.0的設備而且權限沒給的話,就會出現相似以下的問題:android
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官方權限分類:git
以下爲正常的權限:github
ACCESS_LOCATION_EXTRA_COMMANDS
ACCESS_NETWORK_STATE
ACCESS_NOTIFICATION_POLICY
ACCESS_WIFI_STATE
BLUETOOTH
BLUETOOTH_ADMIN
BROADCAST_STICKY
CHANGE_NETWORK_STATE
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
以下爲危險權限:app
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
那麼問題來了,由於這些危險權限致使app在6.0上面崩潰就不該該,因此要找好的方法來解決問題:ide
庫的地址:https://github.com/mylhyl/AndroidAcpgradle
項目中的集成和使用:ui
1,gradle集成:google
compile 'com.mylhyl:acp:1.1.5'
2,代碼集成進來,方便修改spa
我採用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(List<String> permissions) { } }); return true; }
鏈式調用,提供受權和受權失敗兩種狀況處理
若是提供權限會調用onGranted(){}方法,你只須要在裏面添加受權後的操做就行。
若是受權失敗會調用onDenied(){}方法,你能夠提醒用戶受權失敗,並進行後續操做。
而且該庫具備以下特色:
M
版本檢測固然建議代碼集成到項目中來,後期萬一修改也比較方便