android 6.0更新了全新的權(quán)限機(jī)制曹铃,在使用一些涉及用戶隱私的權(quán)限時(shí)瓣赂,會(huì)讓用戶做出選擇是否該app可以使用此權(quán)限鸟缕。
本文章是根據(jù)鴻翔大神的Android 6.0運(yùn)行時(shí)權(quán)限處理完全解析博客思路開(kāi)始學(xué)習(xí)的搀庶。
在查看本文章之前按脚,希望大家可以移步官方開(kāi)發(fā)文檔 使用權(quán)限。在該文章中,詳細(xì)描述了新版本的權(quán)限機(jī)制的更改搬泥,聲明權(quán)限未發(fā)生改變桑寨,需要詳細(xì)觀看在運(yùn)行時(shí)請(qǐng)求權(quán)限,查看權(quán)限最佳做法可以避免糟糕的用戶體驗(yàn)佑钾。
在權(quán)限中西疤,分為Normal Permissions和Dangerous Permissions兩種類別。其中Normal Permissions屬于正常權(quán)限休溶,不需要用戶授權(quán)的;而Dangerous Permissions屬于危險(xiǎn)權(quán)限扰她,與用戶隱私密切相關(guān)兽掰,所以授權(quán)給用戶。我們?cè)诔绦蛑芯托枰獙?duì)這種危險(xiǎn)權(quán)限提供用戶授權(quán)處理徒役,保證app的正常運(yùn)行孽尽。官方文檔給出了正常權(quán)限和危險(xiǎn)權(quán)限,不清楚的可以在官網(wǎng)查看忧勿。
申請(qǐng)權(quán)限的小案例
1).在AndroidManifest中添加權(quán)限杉女。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2).Activity中的具體申請(qǐng)方法(參考官網(wǎng)的課程步驟)
package com.example.mypremission;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
private static final int MY_PERMISSIONS_EXTERNAL_STORAGE_REQ_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
}
public void requestPermission(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//檢查到未授權(quán)該權(quán)限信息
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//上一次用戶拒絕了開(kāi)啟此權(quán)限!
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
//請(qǐng)求權(quán)限
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_EXTERNAL_STORAGE_REQ_CODE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_EXTERNAL_STORAGE_REQ_CODE: {
// 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.
Toast.makeText(this, "申請(qǐng)成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "申請(qǐng)失敗", Toast.LENGTH_SHORT).show();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
運(yùn)行程序:
接下來(lái)我會(huì)研究權(quán)限機(jī)制的封裝,完成權(quán)限批量申請(qǐng)的好辦法鸳吸!