Android组件化开发之动态权限请求封装

Dexter

Github:https://github.com/Karumi/Dexter

依赖引用

1
implementation 'com.karumi:dexter:5.0.0'

封装调用

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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

依赖引用

1
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'

封装调用

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 "";
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :