EasyPermissions 是一個包裝器庫,用於在適用於 Android M 或更高版本時簡化基本系統權限邏輯。
注意:如果您的應用程式是用 Kotlin 編寫的,請考慮使用 easypermissions-ktx 庫,它將 Kotlin 擴充功能新增至核心 EasyPermissions 庫中。
EasyPermissions 是透過將下列相依性新增至build.gradle
檔案來安裝的:
dependencies {
// For developers using AndroidX in their applications
implementation ' pub.devrel:easypermissions:3.0.0 '
// For developers using the Android Support Library
implementation ' pub.devrel:easypermissions:2.0.1 '
}
要開始使用 EasyPermissions,請讓您的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 );
}
}
以下的範例顯示如何為需要CAMERA
和ACCESS_FINE_LOCATION
權限的方法請求權限。有幾點要注意:
EasyPermissions#hasPermissions(...)
檢查應用程式是否已具有所需的權限。此方法可以將任意數量的權限作為其最終參數。EasyPermissions#requestPermissions
請求權限。此方法將請求系統權限並在必要時顯示提供的理由字串。提供的請求代碼對於該請求應該是唯一的,並且該方法可以將任意數量的權限作為其最終參數。AfterPermissionGranted
註解。這是可選的,但為了方便而提供。如果給定請求中的所有權限都被授予,則所有使用正確的請求代碼註解的方法都將被執行(確保具有唯一的請求代碼)。附註解的方法必須為void且沒有輸入參數(相反,您可以使用onSaveInstanceState來保持抑制參數的狀態)。這是為了簡化在授予請求方法的所有權限後需要執行請求方法的常見流程。這也可以透過在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 );
}
}
或者為了更好地控制基本原理對話框,請使用PermissionRequest
:
EasyPermissions . requestPermissions (
new PermissionRequest . Builder ( this , RC_CAMERA_AND_LOCATION , perms )
. setRationale ( R . string . camera_and_location_rationale )
. setPositiveButtonText ( R . string . rationale_ask_ok )
. setNegativeButtonText ( R . string . rationale_ask_cancel )
. setTheme ( R . style . my_fancy_style )
. build ());
或者,為了更好的控制,您可以讓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
// ...
}
}
在某些情況下,如果沒有某些權限,您的應用程式將無法正常運作。如果使用者使用「不再詢問」選項拒絕這些權限,您將無法向使用者要求這些權限,並且必須在應用程式設定中變更這些權限。在這種情況下,您可以使用EasyPermissions.somePermissionPermanentlyDenied(...)
方法向使用者顯示一個對話框,並將他們導向您的應用程式的系統設定畫面:
注意:由於Android框架權限API提供的資訊有限, somePermissionPermanentlyDenied
方法僅在權限被拒絕且您的應用程式收到onPermissionsDenied
回調後才起作用。否則圖書館無法區分永久拒絕和「尚未拒絕」的情況。
@ 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 ();
}
}
如果您想與基本原理對話框交互,請實作EasyPermissions.RationaleCallbacks
。
@ Override
public void onRationaleAccepted ( int requestCode ) {
// Rationale accepted to request some permissions
// ...
}
@ Override
public void onRationaleDenied ( int requestCode ) {
// Rationale denied to request some permissions
// ...
}
基本原理回呼並不一定意味著權限變更。要檢查這些,請參閱EasyPermissions.PermissionCallbacks
。
Copyright 2017 Google
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.