PermissionsDispatcher
4.8.0
PermissionsDispatcher 提供了一個簡單的基於註解的 API 來處理執行時間權限。
該程式庫減輕了編寫一堆檢查語句(無論是否已獲得您的許可)所帶來的負擔,以保持程式碼的乾淨和安全。
這是一個最小的範例,其中您註冊了一個需要Manifest.permission.CAMERA
的MainActivity
。
將以下行加入AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
PermissionsDispatcher 僅引入了一些註釋,使其通用 API 保持簡潔:
注意:註解的方法不能是
private
。
註解 | 必需的 | 描述 |
---|---|---|
@RuntimePermissions | ✓ | 註冊一個Activity 或Fragment 來處理權限 |
@NeedsPermission | ✓ | 註解執行需要一個或多個權限的操作的方法 |
@OnShowRationale | 註解一個方法來解釋為什麼需要權限。它會傳入一個PermissionRequest 對象,該物件可用於根據使用者輸入繼續或中止目前權限請求。如果您沒有為方法指定任何參數,編譯器將產生process${NeedsPermissionMethodName}ProcessRequest 並cancel${NeedsPermissionMethodName}ProcessRequest 。您可以使用這些方法來取代PermissionRequest (例如:使用DialogFragment ) | |
@OnPermissionDenied | 註解一個方法,如果使用者未授予權限,則呼叫該方法 | |
@OnNeverAskAgain | 註釋如果使用者選擇讓裝置「不再詢問」權限則呼叫的方法 |
@RuntimePermissions
class MainActivity : AppCompatActivity (), View.OnClickListener {
@NeedsPermission( Manifest .permission. CAMERA )
fun showCamera () {
supportFragmentManager.beginTransaction()
.replace( R .id.sample_content_fragment, CameraPreviewFragment .newInstance())
.addToBackStack( " camera " )
.commitAllowingStateLoss()
}
@OnShowRationale( Manifest .permission. CAMERA )
fun showRationaleForCamera ( request : PermissionRequest ) {
showRationaleDialog( R .string.permission_camera_rationale, request)
}
@OnPermissionDenied( Manifest .permission. CAMERA )
fun onCameraDenied () {
Toast .makeText( this , R .string.permission_camera_denied, Toast . LENGTH_SHORT ).show()
}
@OnNeverAskAgain( Manifest .permission. CAMERA )
fun onCameraNeverAskAgain () {
Toast .makeText( this , R .string.permission_camera_never_askagain, Toast . LENGTH_SHORT ).show()
}
}
現在產生的函數比 Java 版本更簡潔直覺!
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setContentView( R .layout.activity_main)
findViewById( R .id.button_camera).setOnClickListener {
// NOTE: delegate the permission handling to generated function
showCameraWithPermissionCheck()
}
}
override fun onRequestPermissionsResult ( requestCode : Int , permissions : Array < String >, grantResults : IntArray ) {
super .onRequestPermissionsResult(requestCode, permissions, grantResults)
// NOTE: delegate the permission handling to generated function
onRequestPermissionsResult(requestCode, grantResults)
}
查看範例以了解更多詳細資訊。
筆記:
若要將 PermissionsDispatcher 新增至您的項目,請在應用程式模組build.gradle
檔案中包含以下內容:
${latest.version}
是
dependencies {
implementation " com.github.permissions-dispatcher:permissionsdispatcher: ${ latest.version } "
annotationProcessor " com.github.permissions-dispatcher:permissionsdispatcher-processor: ${ latest.version } "
}
使用科特林:
apply plugin : ' kotlin-kapt '
dependencies {
implementation " com.github.permissions-dispatcher:permissionsdispatcher: ${ latest.version } "
kapt " com.github.permissions-dispatcher:permissionsdispatcher-processor: ${ latest.version } "
}
Copyright 2016 Shintaro Katafuchi, Marcel Schnelle, Yoshinori Isogai
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.