參考 :http://blog.csdn.net/lmj623565791/article/details/50709663
https://developer.android.com/training/permissions/requesting.html?hl=zh-cn
Check For Permissions
If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. The user is always free to revoke the permission, so even if the app used the camera yesterday, it can't assume it still has that permission today.
To check if you have a permission, call the [ContextCompat.checkSelfPermission()](https://developer.android.com/reference/android/support/v4/content/ContextCompat.html?hl=zh-cn#checkSelfPermission(android.content.Context, java.lang.String))method. For example, this snippet shows how to check if the activity has permission to write to the calendar:
// Assume thisActivity is the current activity
int permissionCheck =
ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
If the app has the permission, the method returns PackageManager.PERMISSION_GRANTED
, and the app can proceed with the operation. If the app does not have the permission, the method returns PERMISSION_DENIED,and the app has to explicitly ask the user for permission.
boolean hasPermision =
ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
6.0 運(yùn)行時(shí)權(quán)限的變化及特點(diǎn)
對(duì)于6.0以下的權(quán)限及在安裝的時(shí)候春弥,根據(jù)權(quán)限聲明產(chǎn)生一個(gè)權(quán)限列表,用戶只有在同意之后才能完成app的安裝叠荠,造成了我們想要使用某個(gè)app匿沛,就要默默忍受其一些不必要的權(quán)限(比如是個(gè)app都要訪問通訊錄、短信等)榛鼎。而在6.0以后逃呼,我們可以直接安裝鳖孤,當(dāng)app需要我們授予不恰當(dāng)?shù)臋?quán)限的時(shí)候,我們可以予以拒絕(比如:?jiǎn)螜C(jī)的象棋對(duì)戰(zhàn)抡笼,請(qǐng)求訪問任何權(quán)限苏揣,我都是不同意的)。當(dāng)然你也可以在設(shè)置界面對(duì)每個(gè)app的權(quán)限進(jìn)行查看推姻,以及對(duì)單個(gè)權(quán)限進(jìn)行授權(quán)或者解除授權(quán)平匈。
新的權(quán)限機(jī)制更好的保護(hù)了用戶的隱私,Google將權(quán)限分為兩類藏古,一類是Normal Permissions吐葱,這類權(quán)限一般不涉及用戶隱私,是不需要用戶進(jìn)行授權(quán)的校翔,比如手機(jī)震動(dòng)、訪問網(wǎng)絡(luò)等灾前;另一類是Dangerous Permission防症,一般是涉及到用戶隱私的,需要用戶進(jìn)行授權(quán)哎甲,比如讀取sdcard蔫敲、訪問通訊錄等。
可以通過
adb shell pm list permissions -d -g
進(jìn)行查看炭玫。
toTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ContextCompat.checkSelfPermission(MainActivity2.this,
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED){
if (MPermissions.shouldShowRequestPermissionRationale(MainActivity2.this, Manifest.permission.READ_SMS, REQUECT_CODE_SDCARD)){
}else{
MPermissions.requestPermissions(MainActivity2.this, REQUECT_CODE_SDCARD, Manifest.permission.READ_SMS);
}
}else{
Toast.makeText(MainActivity2.this, "已經(jīng)有權(quán)限", Toast.LENGTH_SHORT).show();
}
}
})
彈出提示框 通過注解 @ShowRequestPermissionRationale
@ShowRequestPermissionRationale(REQUECT_CODE_SDCARD)
public void showdialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
builder.setTitle("說明")
.setMessage("需要使用電話權(quán)限奈嘿,進(jìn)行電話測(cè)試")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MPermissions.requestPermissions(MainActivity2.this, REQUECT_CODE_SDCARD, Manifest.permission.READ_SMS);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
})
.create()
.show();
}
Android M 即6.0 需要權(quán)限時(shí) APP 系統(tǒng)會(huì)申請(qǐng)相應(yīng)的權(quán)限
在Manifast 中需要寫入權(quán)限,如果沒有在Manifast 中寫入權(quán)限吞加,進(jìn)行權(quán)限授予時(shí)會(huì)直接顯示拒絕授權(quán)裙犹;APP運(yùn)行需要權(quán)限時(shí)要檢查是否被授予權(quán)限
[ContextCompat.checkSelfPermission()](http://developer.android.com/reference/android/support/v4/content/ContextCompat.html#checkSelfPermission(android.content.Context, java.lang.String)) 先檢查是否授權(quán) ,設(shè)置為false衔憨,默認(rèn)是未授權(quán)的叶圃,然后進(jìn)行權(quán)限申請(qǐng),
如果拒絕践图,第二次進(jìn)行權(quán)限申請(qǐng)時(shí)會(huì)彈出為什么要用此權(quán)限
點(diǎn)擊確認(rèn)后掺冠,再次彈出