1.權(quán)限概述
???在Google發(fā)出Android 6.0版本以后泽篮,對(duì)權(quán)限這邊做了很大的變動(dòng)公壤,在開(kāi)發(fā)上不僅需要和原來(lái)一樣在Manifests文件中進(jìn)行權(quán)限的注冊(cè)早歇,而且在申請(qǐng)一些較危險(xiǎn)的權(quán)限時(shí)位谋,還需要在代碼中進(jìn)行動(dòng)態(tài)的申請(qǐng)论衍,不然程序在執(zhí)行時(shí)沒(méi)有相應(yīng)的權(quán)限就會(huì)無(wú)響應(yīng)甚至導(dǎo)致APP崩潰,因此對(duì)于Android 6.0 權(quán)限這邊自己做一個(gè)小小的總結(jié)楣责,防止以后再次入坑
2.權(quán)限分類及區(qū)別
Google對(duì)于Android 6.0 系統(tǒng)權(quán)限分為兩個(gè)級(jí)別:
Normal Permission:普通權(quán)限 這類權(quán)限不會(huì)潛藏有侵害用戶隱私和安全的問(wèn)題竣灌,比如聂沙,訪問(wèn)網(wǎng)絡(luò)的權(quán)限,訪問(wèn)WIFI的權(quán)限等;
android.permission.ACCESS LOCATIONEXTRA_COMMANDS
android.permission.ACCESS NETWORKSTATE
android.permission.ACCESS NOTIFICATIONPOLICY
android.permission.ACCESS WIFISTATE
android.permission.ACCESS WIMAXSTATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE NETWORKSTATE
android.permission.CHANGE WIFIMULTICAST_STATE
android.permission.CHANGE WIFISTATE
android.permission.CHANGE WIMAXSTATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND STATUSBAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET PACKAGESIZE
android.permission.INTERNET
android.permission.KILL BACKGROUNDPROCESSES
android.permission.MODIFY AUDIOSETTINGS
android.permission.NFC
android.permission.READ SYNCSETTINGS
android.permission.READ SYNCSTATS
android.permission.RECEIVE BOOTCOMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST INSTALLPACKAGES
android.permission.SET TIMEZONE
android.permission.SET_WALLPAPER
android.permission.SET WALLPAPERHINTS
android.permission.SUBSCRIBED FEEDSREAD
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE SYNCSETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
Dangerous Permissions: 即危險(xiǎn)權(quán)限初嘹,這類權(quán)限會(huì)直接的威脅到用戶的安全和隱私問(wèn)題及汉,比如說(shuō)訪問(wèn)短信,相冊(cè)屯烦,內(nèi)存訪問(wèn)等權(quán)限坷随;
SMS(短信)
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE(存儲(chǔ)卡)
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
CONTACTS(聯(lián)系人)
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
PHONE(手機(jī))
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
CALENDAR(日歷)
READ_CALENDAR
WRITE_CALENDAR
CAMERA(相機(jī))
CAMERA
LOCATION(位置)
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
SENSORS(傳感器)
BODY_SENSORS
MICROPHONE(麥克風(fēng))
RECORD_AUDIO
普通權(quán)限和危險(xiǎn)權(quán)限的區(qū)別:
- 普通權(quán)限:?jiǎn)螚l權(quán)限,可以直接在Manifests文件中進(jìn)行注冊(cè)從而獲取權(quán)限驻龟,在程序初次安裝啟動(dòng)時(shí)直接獲取用戶權(quán)限
- 危險(xiǎn)權(quán)限:成組授予温眉,當(dāng)授予一組中任何一個(gè)權(quán)限時(shí),該組內(nèi)其他的權(quán)限也會(huì)自動(dòng)被授予迅脐。不僅需要在Manifests文件中進(jìn)行注冊(cè)還要在代碼中進(jìn)行動(dòng)態(tài)申請(qǐng)芍殖。
3.權(quán)限申請(qǐng)相關(guān)API
3.1 在Activity中進(jìn)行6.0權(quán)限申請(qǐng)相關(guān)API
1. ContextCompat.checkSelfPermission() 判斷權(quán)限是否已經(jīng)授予
2. ActivityCompat.requestPermissions() 申請(qǐng)權(quán)限
3. ActivityCompat.OnRequestPermissionsResultCallback 申請(qǐng)權(quán)限的回調(diào)
4. ActivityCompat.shouldShowRequestPermissionRationale() 向用戶展示所需權(quán)限的理由
/**
* shouldShowRequestPermissionRationale 三種情景
* 1. 如果用戶在過(guò)去拒絕了權(quán)限請(qǐng)求,并在權(quán)限請(qǐng)求系統(tǒng)對(duì)話框中選擇了 Don’t ask again 選項(xiàng) 返回false
* 2. 如果設(shè)備規(guī)范禁止應(yīng)用具有該權(quán)限 返回false
* 3. 如果應(yīng)用在之前已經(jīng)請(qǐng)求過(guò)此權(quán)限但用戶拒絕了請(qǐng)求谴蔑,返回 true
*/
3.2 在Fragment中進(jìn)行6.0權(quán)限申請(qǐng)相關(guān)API
1. FragmentCompat.requestPermissions()
2. FragmentCompat.shouldShowRequestPermissionRationale()
4.動(dòng)態(tài)請(qǐng)求權(quán)限的一般步驟
在AndroidManifests文件中添加相應(yīng)的權(quán)限
-
檢查權(quán)限
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { }else{ ...... } ContextCompat.checkSelfPermission豌骏,主要用于檢測(cè)某個(gè)權(quán)限是否已經(jīng)被授予,方法返回值為PackageManager.PERMISSION_DENIED或者PackageManager.PERMISSION_GRANTED隐锭。當(dāng)返回DENIED就需要進(jìn)行申請(qǐng)授權(quán)了
-
申請(qǐng)權(quán)限
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); 該方法是異步的窃躲,第一個(gè)參數(shù)是Context;第二個(gè)參數(shù)是需要申請(qǐng)的權(quán)限的字符串?dāng)?shù)組钦睡;第三個(gè)參數(shù)為requestCode蒂窒,主要用于回調(diào)的時(shí)候檢測(cè)≤衽可以從方法名requestPermissions以及第二個(gè)參數(shù)看出洒琢,是支持一次性申請(qǐng)多個(gè)權(quán)限的,系統(tǒng)會(huì)通過(guò)對(duì)話框逐一詢問(wèn)用戶是否授權(quán)
-
處理權(quán)限申請(qǐng)的回調(diào)
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } } } 對(duì)于權(quán)限的申請(qǐng)結(jié)果褐桌,首先驗(yàn)證requestCode定位到你的申請(qǐng)衰抑,然后驗(yàn)證grantResults對(duì)應(yīng)于申請(qǐng)的結(jié)果,這里的數(shù)組對(duì)應(yīng)于申請(qǐng)時(shí)的第二個(gè)權(quán)限字符串?dāng)?shù)組荧嵌。如果你同時(shí)申請(qǐng)兩個(gè)權(quán)限呛踊,那么grantResults的length就為2,分別記錄你兩個(gè)權(quán)限的申請(qǐng)結(jié)果
5.建立屬于自己的權(quán)限請(qǐng)求框架CommonPermissionFrame
對(duì)于權(quán)限的動(dòng)態(tài)請(qǐng)求一般需要傳入三個(gè)參數(shù)基本就可以進(jìn)行權(quán)限的請(qǐng)求啦撮,采用了注解+反射的方式搭建
- Activity/Fragment: 調(diào)用相應(yīng)的方法
- requestCode: 在進(jìn)行權(quán)限申請(qǐng)回調(diào)時(shí)谭网,對(duì)應(yīng)于相應(yīng)的權(quán)限
- requestPermissions:所需要的申請(qǐng)的權(quán)限
- PermissionDes:二次進(jìn)行權(quán)限申請(qǐng)時(shí),向用戶說(shuō)明權(quán)限申請(qǐng)的理由(非必須赃春,一般作為彈窗出現(xiàn))