Android 6.0一些特殊權(quán)限需在應(yīng)用運(yùn)行執(zhí)行要使用的權(quán)限時(shí)才提醒獲取該權(quán)限宠页。首先看那些特許權(quán)限需要特許處理呢!
Calendar日歷讀寫(xiě)相關(guān)烤宙;照相機(jī)近她;聯(lián)系人粘捎;地理位置泳桦;Microphone系統(tǒng)聲音,Phone手機(jī)狀態(tài)亲轨、撥打電話等;Sensors傳感器厘唾;SMS信息讀寫(xiě)鹤树,Storage文件存儲(chǔ);有用到這些功能都需要在使用時(shí)代碼進(jìn)行權(quán)限注冊(cè)逊朽。如何注冊(cè):
撥打電話
public void onCall(String mobile){
this.mMobile = mobile;
if (Build.VERSION.SDK_INT >= 23) {
int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext,Manifest.permission.CALL_PHONE);
if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(mContext,new String[]{Manifest.permission.CALL_PHONE},REQUEST_CODE_ASK_CALL_PHONE);
return;
}else{
//上面已經(jīng)寫(xiě)好的撥號(hào)方法
callDirectly(mobile);
}
} else {
//上面已經(jīng)寫(xiě)好的撥號(hào)方法
callDirectly(mobile);
}
}
那么用戶點(diǎn)擊拒絕或者允許魂迄,我們?cè)趺床拍苣玫交卣{(diào)呢,如果能拿到回調(diào)惋耙,我們就可以根據(jù)用戶的選擇來(lái)執(zhí)行不同的操作了捣炬。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_CALL_PHONE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
callDirectly(mobile);
} else {
// Permission Denied
Toast.makeText(MainActivity.this, "CALL_PHONE Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
關(guān)于AndroidM上權(quán)限的動(dòng)態(tài)獲取熊昌,這里只給出了一個(gè)最簡(jiǎn)單的示例,如果你還沒(méi)有盡興湿酸,那么下面這篇國(guó)外的博文婿屹,一定會(huì)讓你滿足。
Everything every Android Developer must know about new Android’s Runtime Permission