原文地址 http://blog.magicer.xyz/2017/...html
在Android 6.0以前,開發Android應用程序的時,開發者須要使用什麼權限只須要在AndroidManifest.xml
文件中顯示的聲明便可。以下:java
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
可是,在Android 6.0以後,Android對應用程序的權限進行了進一步的規範。分出來危險權限和通常權限,當咱們須要危險權限的時候須要在運行時進行受權。這樣作也有好處。對於一些應用用戶的選擇更多了。android
官方權限教程
當咱們須要用到危險權限時,咱們就須要動態的申請權限,那麼危險權限有哪些呢?api
對於常規的權限咱們只須要在清單文件中註冊便可,好比網絡權限。網絡
若是您的應用須要危險權限,則每次執行須要這一權限的操做時您都必須檢查本身是否具備該權限。用戶始終能夠自由調用此權限,所以,即便應用昨天使用了相機,它不能假設本身今天仍具備該權限。app
要檢查您是否具備某項權限,請調用 ContextCompat.checkSelfPermission()
方法。例如,如下代碼段顯示瞭如何檢查 Activity 是否具備在日曆中進行寫入的權限:ide
// Assume thisActivity is the current activity int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);
若是應用具備此權限,方法將返回 PackageManager.PERMISSION_GRANTED
,而且應用能夠繼續操做。若是應用不具備此權限,方法將返回 PERMISSION_DENIED
,且應用必須明確向用戶要求權限。函數
以上是官方文檔中關於檢測權限的說明。
先來個小demo。看下怎麼動態的請求權限,也能夠看官方文檔中的介紹。ui
private static final int PERMISSION_REQUEST_CODE = 1; //權限請求碼 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //獲取權限後的操做。讀取文件 }else { //請求權限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } } /** * 檢測權限是否受權 * @return */ private boolean checkPermission(Context context, String permission) { return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context,permission); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case PERMISSION_REQUEST_CODE: if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //獲得了受權 Toast.makeText(this, "受權成功", Toast.LENGTH_SHORT).show(); }else { //未受權 Toast.makeText(this, "受權失敗", Toast.LENGTH_SHORT).show(); } break; default: break; } }
當用戶點擊了禁止
按鈕後,當咱們下次還須要該權限怎麼辦? shouldShowRequestPermissionRationale()函數會在用戶點擊了禁止後返回true
,咱們能夠在該方法返回true時在提示用戶開啓權限。
代碼跟上面的差很少。能夠簡要看一下。this
private static final int PERMISSION_REQUEST_CODE = 1; //權限請求碼 private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.activity_main_storage_btn); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { storagePermission(); } }); } private void storagePermission() { if (checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //獲取權限後的操做。讀取文件 }else { // //請求權限 // ActivityCompat.requestPermissions(this, // new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, // PERMISSION_REQUEST_CODE); if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this, "須要開啓存儲權限", Toast.LENGTH_SHORT).show(); showRequestPermissionDialog(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE); }else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); } } } private void showRequestPermissionDialog(final String[] permissions, final int requestCode) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("使用該功能須要使用SD卡權限\n是否再次開啓權限"); builder.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(MainActivity.this,permissions,requestCode); } }); builder.setNegativeButton("否",null); builder.setCancelable(true); builder.show(); } /** * 檢測權限是否受權 * @return */ private boolean checkPermission(Context context, String permission) { return PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(context,permission); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case PERMISSION_REQUEST_CODE: if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //獲得了受權 Toast.makeText(this, "受權成功", Toast.LENGTH_SHORT).show(); }else { //未受權 Toast.makeText(this, "受權失敗", Toast.LENGTH_SHORT).show(); } break; default: break; } }