Android组件化开发之动态权限请求封装
Dexter
Github:https://github.com/Karumi/Dexter
依赖引用
implementation 'com.karumi:dexter:5.0.0'
封装调用
DexterPermissionHelper.requestPermissions(this, new DexterPermissionHelper.PermissionsCheckListener() {
@Override
public void onPermissionsGranted(int permissionType) {
Toast.makeText(MainActivity.this, "已获得权限", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionsDenied(int permissionType) {
}
});
封装实现
DexterPermissionHelper.java
package me.yezhou.helper.permission;
import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.util.Log;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import java.util.List;
/**
* Created by yezhou on 2017/11/6.
*/
public class DexterPermissionHelper {
private static final String TAG = "yezhou";
public static final int BASE_PERMISSIONS = 0;
public static final int SCAN_PERMISSIONS = 1;
public static final int TAKE_PHOTO_PERMISSIONS = 2;
private static final String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
private static final String[] scanPermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
private static final String[] takePhotoPermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
public static void requestPermissions(Activity activity, PermissionsCheckListener listener) {
requestPermissions(activity, permissions, listener, BASE_PERMISSIONS);
}
public static void requestScanPermissions(Activity activity, PermissionsCheckListener listener) {
requestPermissions(activity, scanPermissions, listener, SCAN_PERMISSIONS);
}
public static void requestTakePhotoPermissions(Activity activity, PermissionsCheckListener listener) {
requestPermissions(activity, takePhotoPermissions, listener, TAKE_PHOTO_PERMISSIONS);
}
public static void requestPermissions(Activity activity, final String[] permissions, final PermissionsCheckListener listener, final int permissionType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Dexter.withActivity(activity)
.withPermissions(permissions)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
for (PermissionGrantedResponse grantedResponse : report.getGrantedPermissionResponses()) {
Log.i(TAG, "已获得权限: " + grantedResponse.getPermissionName());
}
for (PermissionDeniedResponse deniedResponse : report.getDeniedPermissionResponses()) {
Log.i(TAG, "权限被拒绝: " + deniedResponse.getPermissionName());
}
if (listener != null) {
Log.i(TAG, "report.getGrantedPermissionResponses().size(): " + report.getGrantedPermissionResponses().size() + ", permissions.length: " + permissions.length);
if (report.getGrantedPermissionResponses().size() == permissions.length) {
listener.onPermissionsGranted(permissionType);
} else {
listener.onPermissionsDenied(permissionType);
}
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> list, PermissionToken permissionToken) {
}
}).check();
}
}
public interface PermissionsCheckListener {
void onPermissionsGranted(int permissionType);
void onPermissionsDenied(int permissionType);
}
}
RxPermissions
Github:https://github.com/tbruyelle/RxPermissions
依赖引用
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
封装调用
RxPermissionHelper.requestPermissions(this, new RxPermissionHelper.PermissionsCheckListener() {
@Override
public void onPermissionsGranted(int permissionType) {
Toast.makeText(MainActivity.this, "已获得权限", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionsDenied(int permissionType) {
}
});
封装实现
DexterPermissionHelper.java
package me.yezhou.helper.permission;
import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.tbruyelle.rxpermissions2.RxPermissions;
import io.reactivex.functions.Consumer;
/**
* Created by yezhou on 2017/11/6.
*/
public class RxPermissionHelper {
private static final String TAG = "yezhou";
public static final int BASE_PERMISSIONS = 0;
public static final int SCAN_PERMISSIONS = 1;
public static final int TAKE_PHOTO_PERMISSIONS = 2;
private static final String[] permissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
};
private static final String[] scanPermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
private static final String[] takePhotoPermissions = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};
public static void requestPermissions(FragmentActivity activity, PermissionsCheckListener listener) {
requestPermissions(activity, permissions, listener, BASE_PERMISSIONS);
}
public static void requestPermissions(Fragment fragment, PermissionsCheckListener listener) {
requestPermissions(fragment, permissions, listener, BASE_PERMISSIONS);
}
public static void requestScanPermissions(FragmentActivity activity, PermissionsCheckListener listener) {
requestPermissions(activity, scanPermissions, listener, SCAN_PERMISSIONS);
}
public static void requestScanPermissions(Fragment fragment, PermissionsCheckListener listener) {
requestPermissions(fragment, scanPermissions, listener, SCAN_PERMISSIONS);
}
public static void requestTakePhotoPermissions(FragmentActivity activity, PermissionsCheckListener listener) {
requestPermissions(activity, takePhotoPermissions, listener, TAKE_PHOTO_PERMISSIONS);
}
public static void requestTakePhotoPermissions(Fragment Fragment, PermissionsCheckListener listener) {
requestPermissions(Fragment, takePhotoPermissions, listener, TAKE_PHOTO_PERMISSIONS);
}
@SuppressLint("CheckResult")
public static void requestPermissions(FragmentActivity activity, final String[] permissions, final PermissionsCheckListener listener, final int permissionType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
RxPermissions rxPermissions = new RxPermissions(activity);
rxPermissions.request(permissions)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean isGranted) {
if (isGranted) {
Log.i(TAG, "已获得权限: " + RxPermissionHelper.toString(permissions));
if (listener != null) {
listener.onPermissionsGranted(permissionType);
}
} else {
Log.i(TAG, "权限被拒绝: " + RxPermissionHelper.toString(permissions));
if (listener != null) {
listener.onPermissionsDenied(permissionType);
}
}
}
});
}
}
@SuppressLint("CheckResult")
public static void requestPermissions(Fragment fragment, final String[] permissions, final PermissionsCheckListener listener, final int permissionType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
RxPermissions rxPermissions = new RxPermissions(fragment);
rxPermissions.request(permissions)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean isGranted) {
if (isGranted) {
Log.i(TAG, "已获得权限: " + RxPermissionHelper.toString(permissions));
if (listener != null) {
listener.onPermissionsGranted(permissionType);
}
} else {
Log.i(TAG, "权限被拒绝: " + RxPermissionHelper.toString(permissions));
if (listener != null) {
listener.onPermissionsDenied(permissionType);
}
}
}
});
}
}
public interface PermissionsCheckListener {
void onPermissionsGranted(int permissionType);
void onPermissionsDenied(int permissionType);
}
private static String toString(String[] permissions) {
if (permissions != null && permissions.length > 0) {
StringBuilder sBuilder = new StringBuilder();
sBuilder.append('[');
for (int i = 0; i < permissions.length; i++) {
if (i == 0) {
sBuilder.append(permissions[i]);
} else {
sBuilder.append(", ");
sBuilder.append(permissions[i]);
}
}
sBuilder.append(']');
return sBuilder.toString();
}
return "";
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/dynamic-permission-request-encapsulation-for-android-componentization-development/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
Android组件化开发之动态权限请求封装
Dexter
Github:https://github.com/Karumi/Dexter
依赖引用
implementation 'com.karumi:dexter:5.0.0'
封装调用
DexterPermissionHelper.request……
文章目录
关闭
共有 0 条评论