| 網站首頁 | 下載中心 | 產品展示 | 技術文章 | 技術論壇 | VIP專區 | 
您現在的位置: 南域劍盟 >> 技術文章 >> 最新動態 >> 文獻正文
專 題 欄 目
最 新 熱 門
最 新 推 薦
相 關 文 章
Android攻城獅一二季
79天蛻變黑馬Android安卓系統軟件開發工
Android工具箱
極客學院安卓Android全套最新視頻教程
Android 4.0網絡編程詳解
讓Android運行在Windows操作系統上
Android軟件安全與逆向分析
android教學、資料,工具
老羅Android第二季
android教程合集+源碼
Android6.0權限申請庫         ★★★
Android6.0權限申請庫
作者:佚名 文章來源:網絡 點擊數: 更新 時間:2017-07-09 16:54:39

動態申請權限庫:easypermissions使用
當接觸了Android 6.0 7.0 之后,很多人就會發現,單純的在AndroidManifest.xml中聲明權限已經是不好使的了,因為Google為了保護用戶的權限,需要應用動態的權限申請。

下面我們就介紹一個Google官方放出來的動態申請權限的庫,GitHub地址,這個庫可以簡單的幫助我們完成動態申請權限的整個流程,并且可以將結果回調回來,因此我們可以在回調結果中加上我們的處理邏輯。

Installation 安裝

EasyPermissions is installed by adding the following dependency to your build.gradle file:

dependencies {     compile 'pub.devrel:easypermissions:0.4.0' }
Usage 用法

Basic 基礎

To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResult method:

開始用這個庫的時候,需要讓Activity或者Fragment重寫onRequestPermissionsResult這個方法:

public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      @Override     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {         super.onRequestPermissionsResult(requestCode, permissions, grantResults);          // Forward results to EasyPermissions         // 遠期回調回來的結果         EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);     } }
Request Permissions

The example below shows how to request permissions for a method that requires both CAMERA and ACCESS_FINE_LOCATION permissions. There are a few things to note:

Using EasyPermissions#hasPermissions(…) to check if the app already has the required permissions. This method can take any number of permissions as its final argument.

Requesting permissions with EasyPermissions#requestPermissions. This method will request the system permissions and show the rationale string provided if necessary. The request code provided should be unique to this request, and the method can take any number of permissions as its final argument.

Use of the AfterPermissionGranted annotation. This is optional, but provided for convenience. If all of the permissions in a given request are granted, any methods annotated with the proper request code will be executed. This is to simplify the common flow of needing to run the requesting method after all of its permissions have been granted. This can also be achieved by adding logic on the onPermissionsGranted callback.

下面這個例子展示了如何申請權限用一個方法同時申請相機和申請定位的權限,這里有一些內容需要注意:

用EasyPermissions的hasPermissions方法檢測一下,是否app已經具有要申請的權限。這個方法可以將許多方法作為最后的論證。

申請權限通過EasyPermissions的requestPermissions的方法。這個方法將會申請系統的權限,并且在如果有必要的條件下會展示出理由。這個申請權限的代碼應該提供獨一無二的申請,并且這些方法可以提供很多權限作為最終的定論。

應用AfterPermissionGranted這個標簽,這是可以選擇的,但是它提供了方便。如果所有權限在申請之后都被授予了,一些攜帶著這個標簽的方法將會被執行。這是為了簡化在所有權限被授予后需要運行請求方法的公共流程。這些同樣也可以被實現通過添加合理的邏輯在onPermissionsGranted這個方法回調的時候。

@AfterPermissionGranted(RC_CAMERA_AND_LOCATION) private void methodRequiresTwoPermission() {     String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};     if (EasyPermissions.hasPermissions(this, perms)) {         // Already have permission, do the thing         // ...     } else {         // Do not have permissions, request them now         EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),                 RC_CAMERA_AND_LOCATION, perms);     } }
Optionally, for a finer control, you can have your Activity / Fragment implement the PermissionCallbacks interface.

隨意的,通過一個好的控制,你可以讓你的Activity/Fragment 實現PermissionCallbacks這個接口。

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     }      @Override     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {         super.onRequestPermissionsResult(requestCode, permissions, grantResults);          // Forward results to EasyPermissions         EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);     }      @Override     public void onPermissionsGranted(int requestCode, List<String> list) {         // Some permissions have been granted         // ...     }      @Override     public void onPermissionsDenied(int requestCode, List<String> list) {         // Some permissions have been denied         // ...     } }
Required Permissions 必須的權限

In some cases your app will not function properly without certain permissions. If the user denies these permissions with the “Never Ask Again” option, you will be unable to request these permissions from the user and they must be changed in app settings. You can use the method EasyPermissions.somePermissionPermanentlyDenied(…) to display a dialog to the user in this situation and direct them to the system setting screen for your app:

無論如何你的app在沒有某些權限的時候是不能正常運行的。如果用戶否認這些權限,并且選擇了再也不詢問的選項,你將沒有能力去申請那些權限通過用戶,他們必須在app的設置中修改這些。你可以用這個方法EasyPermissions.somePermissionPermanentlyDenied(…),展示一個對話框給用戶在這個位置直接的告訴他們去系統的設置中去改變為了這個app:

@Override public void onPermissionsDenied(int requestCode, List<String> perms) {     Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());      // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."     // This will display a dialog directing them to enable the permission in app settings.     if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {         new AppSettingsDialog.Builder(this).build().show();     } }  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);      if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {         // Do something after user returned from app settings screen, like showing a Toast.         Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)                 .show();     } }

文獻錄入:admin    責任編輯:admin 
  • 上一篇文獻:

  • 下一篇文獻: 沒有了
  • 【字體: 】【加入收藏】【告訴好友】【打印此文】【關閉窗口
    cba专用篮球molten