Skip to content

Commit

Permalink
完成改造
Browse files Browse the repository at this point in the history
  • Loading branch information
CraftsmanHyj committed Jul 13, 2022
1 parent 313501c commit dc239aa
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 137 deletions.
6 changes: 3 additions & 3 deletions Demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.hyj.permission.demo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
versionCode 4
versionName "2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -50,7 +50,7 @@ android {
//确定输出文件名
def today = new Date()
def path = ((project.name != "app") ? project.name : rootProject.name.replace(" ", "")) + "_" +
variant.flavorName + "_" +
//variant.flavorName + "_" +
variant.buildType.name + "_" +
variant.versionName + "_" +
//today.format('yyyyMMddHHmm') +
Expand Down
142 changes: 28 additions & 114 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
处理需要动态申请的用户权限:

+ 对于拒绝、不再询问有做再次调起的逻辑封装,支持国产定制系统。
+ 支持一次申请当个、多个动态权限;
+ 支持一次申请单个、多个动态权限;
+ 支持拒绝后弹窗询问再次请求及提示语自定义;



[更新日志]( https://github.com/CraftsmanHyj/SimplePermission/blob/master/docs/UpdateLog.md );  [动态权限列表](https://github.com/CraftsmanHyj/SimplePermission/blob/master/docs/%E6%9D%83%E9%99%90%E8%AF%B4%E6%98%8E.md);  [下载Demo体验效果](https://github.com/CraftsmanHyj/SimplePermission/raw/master/docs/Demo.apk)

![](https://github.com/CraftsmanHyj/SimplePermission/blob/master/docs/DemoQR.png)

# 添加依赖

**Step 1.** 在项目的根目录`build.gradle`文件中加入JitPack仓库
Expand Down Expand Up @@ -38,124 +36,40 @@ dependencies {

# 使用示例

**Step 1.** 定义申请权限枚举类,注意构造函数中`code`的赋值方式;

注意够着函数中`code`的赋值方式`this.code = PermConstant.getReqeustCode();`

```java
public enum PermsEnm implements IPermissionInfo {
CAMER(Manifest.permission.CAMERA),
LOCATION_CONTACTS(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS);

private int code;
private String[] perms;

PermsEnm(String... perms) {
this.code = PermConstant.getReqeustCode();
this.perms = perms;
}

@Override
public int getRequestCode() {
return code;
}

@Override
public String[] getPermissions() {
return perms;
}
}
```



**Step 2.** 在基类或使用到的Activity、Fragment中分别注册回调方法

```java
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//用户的允许、拒绝的统一回调
PermissionManager.onRequestPermissionsResult(mActivity, requestCode, permissions, grantResults);
}
```



**Step 3.** 拒绝且不再提示逻辑处理

当执行拒绝且不再提示的逻辑之后,会弹出再次询问,跳转到设置界面去设置权限的逻辑;注册接收返回结果处理回调;

```java
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//从应用权限设置页面返回,可以从这里获取到设置的结果
PermissionManager.onActivityResult(mActivity, requestCode);
}
```



**Step 4.** 执行申请逻辑

**Activity**中使用方法:

```java
PermissionManager.requestPermissions(this, new SimplePermissionCallback<AppCompatActivity>(this) {
@Override
public void onPermissionGranted(int reqeustCode, List<String> perms) {
ToastUtils.showToast(PermissionActivity.this, "activity 已经获取了相机权限");
```kotlin
class MainActivity : AppCompatActivity() {
private val gpsLauncher = registerForGpsResult {
onDenied { showToast("拒绝打开GPS") }
onGranted { showToast("已经打开GPS") }
onRequestTip { "测试逻辑,测试是否可以准确检查GPS状态!" }
}

@Override
public void onPermissionDenied(int reqeustCode, List<String> perms) {
ToastUtils.showToast(PermissionActivity.this, "activity 拒绝使用相机");
}

@Override
public String getPermissionSetMsg(int reqeustCode) {
return "activity 扫码需要使用相机权限";

//需要在lifeCycleOwner start状态前创建Register对象
private val multiPermissionLauncher = registerForPermissionResult {
onGranted { showToast("多权限:同意了权限请求") }
onDenied { showToast("多权限:拒绝了权限") }
onPermanentlyDeniedTip { "多权限:运行APP需要这几个权限,请授权。" }
}
}, PermsEnm.CAMER);
```
......

**Fragment**中使用方法
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(mBinding.root)

```java
PermissionManager.requestPermissions(getActivity(), new SimplePermissionCallback<Fragment>(this) {
@Override
public void onPermissionGranted(int reqeustCode, List<String> perms) {
ToastUtils.showToast(getContext(), "frame fragment 已经获取了相机权限");
initListener()
}

@Override
public void onPermissionDenied(int reqeustCode, List<String> perms) {
ToastUtils.showToast(getContext(), "frame fragment 拒绝使用相机");
}
private fun initListener() = mBinding.run {
btnCheckGPSStatus.setOnClickListener { gpsLauncher.launch() }

@Override
public String getPermissionSetMsg(int reqeustCode) {
return "frame fragment 扫码需要使用相机权限";
btnMultiPermission.setOnClickListener {
val multiplePermission = arrayOf(CAMERA, READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE)
multiPermissionLauncher.launchP(this@MainActivity, *multiplePermission)
}

......
}
}, PermsEnm.CAMER);
}
```



在注册了`onRequestPermissionsResult``onActivityResult`两个方法之后,不同的操作会对应到SimplePermissionCallback的不同的回调方法上:

+ onPermissionGranted调用场景:

+ 用户点击允许
+ 弹出跳转设置框,点击设置,选择允许后返回

+ onPermissionDenied调用场景:

+ 用户点击禁止
+ 弹出跳转设置框,点击取消
+ 弹出跳转设置框,点击设置,跳转后,未选择允许此权限,返回界面

+ getPermissionSetMsg调用场景:

勾选了“不在询问”之后,弹出授权提示Dialog时候的提示语
Binary file removed docs/DemoQR.png
Binary file not shown.
Binary file added docs/Demo_release_2.1_.apk
Binary file not shown.
40 changes: 20 additions & 20 deletions docs/UpdateLog.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
# 更新日志

## 4_1.0.4 2022-07-13

1. sdk更新为kotlin实现
2. 增加了蓝牙、安装包、悬浮窗、系统设置、忽扰权限、电池优化的权限申请兼容处理
3. 权限申请的方式修改为`registerForActivityResult`的方式

## 1.0.3 2020-03-03
## 3_1.0.3 2020-03-03

1. 增加GPS定位检测功能
```PermissionManager.openGPSLocation()```
2. 小米、华为、oppo、vivo、魅族手机支持直接跳转到权限授权界面
3. 优化已知bug

## 2_1.0.2 2019-11-09

1. PermissionCallback回调方法修改为IPermissionCallback

## 1.0.2 2019-11-09
2. PermissionCallbackImpl方法修改为SimplePermissionCallback

+ PermissionCallback回调方法修改为IPermissionCallback
3. 添加判断是否有权限的方法:hasPermissions(@NonNull Activity activity, IPermissionInfo permInfo)方法

+ PermissionCallbackImpl方法修改为SimplePermissionCallback
4. 添加请求权限方法:requestPermissions(@NonNull Activity activity, @NonNull IPermissionCallback callback, int requestCode, String... perms)

+ 添加判断是否有权限的方法:hasPermissions(@NonNull Activity activity, IPermissionInfo permInfo)方法
5. 添加onActivityResult(Activity activity, int requestCode)回调方法,
在去应用设置界面去设置后,返回后回调此方法,可以处理设置结果

+ 添加请求权限方法:requestPermissions(@NonNull Activity activity, @NonNull IPermissionCallback callback, int requestCode, String... perms)
```java
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri);
Activity.startActivityForResult(intent, REQEUSTCODE);
```

+ 添加onActivityResult(Activity activity, int requestCode)回调方法,
在去应用设置界面去设置后,返回后回调此方法,可以处理设置结果
详情看DEMO

```java
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
intent.setData(uri);
Activity.startActivityForResult(intent, REQEUSTCODE);
```

+ 详情看DEMO



## 1.0.1 2019-09-21
## 1_1.0.1 2019-09-21

+ SimplePermission立项

0 comments on commit dc239aa

Please sign in to comment.