要在Android項目中集成EasyPermission,可以按照以下步驟進行操作:
dependencies {
implementation 'pub.devrel:easypermissions:3.0.0'
}
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int RC_CAMERA_PERM = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
// 已經獲取到相機權限
} else {
// 請求相機權限
EasyPermissions.requestPermissions(
this,
"需要獲取相機權限以拍攝照片",
RC_CAMERA_PERM,
Manifest.permission.CAMERA
);
}
}
@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
// 權限已經授予
}
@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
// 權限被拒絕
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
// 顯示一個對話框讓用戶跳轉到應用權限設置頁面
EasyPermissions.goSettingsDialog(this);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
通過以上步驟,就可以在Android項目中集成EasyPermission并使用其提供的便捷方法來請求和處理權限了。