GitHub地址: https://github.com/kongpf8848/TKPermission
前言
什么是Activity Result API?
Activity Result API 是 Google官方推荐的Activity、Fragment获取返回结果的方式, 提供了用于注册结果、启动结果以及在系统分派结果后对其进行处理的组件 。引入方式如下:
implementation 'androidx.activity:activity:1.2.0-alpha06'
implementation 'androidx.activity:activity-ktx:1.2.0-alpha06'
implementation 'androidx.fragment:fragment:1.3.0-alpha06'
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha06'
主要用到两个组件:
ActivityResultContract:契约,定义了输入数据的类型和输出数据的类型, 定义了如何传递数据和如何处理返回的数据
public abstract class ActivityResultContract<I, O> {
/** Create an intent that can be used for {@link Activity#startActivityForResult} */
public abstract @NonNull Intent createIntent(@NonNull Context context,
@SuppressLint("UnknownNullness") I input);
/** Convert result obtained from {@link Activity#onActivityResult} to O */
@SuppressLint("UnknownNullness")
public abstract O parseResult(int resultCode, @Nullable Intent intent);
......
}
谷歌定义了一系列常用的ActivityResultContract协议
- StartActivityForResult,用于启动新的页面并获取返回结果
- RequestPermission,用于申请单个权限并获取返回结果
- RequestMultiplePermissions,用于申请多个权限并获取返回结果
- GetContent(),用于选择内容并获取返回结果
- PickContact,用于选择联系人并获取返回结果
- TakePicture,用于拍照并获取返回结果
- TakeVideo,用于拍摄视频并获取返回结果
ActivityResultLauncher:启动器,调用ActivityResultLauncher的launch方法来启动页面跳转,作用相当于原来的startActivity()
/**
* A launcher for a previously-{@link ActivityResultCaller#registerForActivityResult prepared call}
* to start the process of executing an {@link ActivityResultContract}.
*
* @param <I> type of the input required to launch
*/
public abstract class ActivityResultLauncher<I> {
/**
* Executes an {@link ActivityResultContract}.
*
* @param input the input required to execute an {@link ActivityResultContract}.
*/
public void launch(@SuppressLint("UnknownNullness") I input) {
launch(input, null);
}
/**
* Executes an {@link ActivityResultContract}.
*
* @param input the input required to execute an {@link ActivityResultContract}.
* @param options Additional options for how the Activity should be started.
*/
public abstract void launch(@SuppressLint("UnknownNullness") I input,
@Nullable ActivityOptionsCompat options);
/**
* Unregisters this launcher, releasing the underlying result callback, and any references
* captured within it.
*
* You should call this if the registry may live longer than the callback registered for this
* launcher.
*/
@MainThread
public abstract void unregister();
/**
* Get the {@link ActivityResultContract} that was used to create this launcher.
*
* @return the contract that was used to create this launcher
*/
@NonNull
public abstract ActivityResultContract<I, ?> getContract();
}
如我们现在需要申请位置权限,旧的权限申请方式如下:
val PERMISSION_REQUEST_LOCATION = 0
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSION_REQUEST_LOCATION
)
然后在onRequestPermissionsResult处理申请结果:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_LOCATION) {
if(grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
}
}
基于Activity Result API的申请方式如下:
registerForActivityResult(RequestPermission()){
/**
* 此处处理申请结果
*/
Toast.makeText(this@ForResultActivity, "result:${it}", Toast.LENGTH_SHORT).show()
}.launch(Manifest.permission.ACCESS_FINE_LOCATION)
是不是一下子简洁了很多,有木有,哈哈_^_
正文
TKPermission是基于AndroidX Activity Result API封装的权限类库,摆脱了旧的繁琐的权限申请方式,让权限申请变得无比简单轻松
功能特点
- 使用简单,调用方式友好,兼容Kotlin和Java,使用Kotlin语言调用更酸爽
- 基于Google最新的Activity Result API封装,代码量极少,极简主义
- 支持一次申请单个权限和多个权限,基本满足各类权限申请使用场景
使用
implementation 'com.github.kongpf8848:tkpermission:1.0.0'
Kotlin
- 一次申请单个权限
permission(Manifest.permission.CAMERA) {
/**
* 权限允许时回调
*/
granted {
}
/**
* 权限拒绝时回调
*/
denied {
}
}
- 一次申请多个权限
permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO) {
/**
* 多个权限全部被允许时回调
*/
allGranted {
}
/**
* 被拒绝的权限列表
*/
denied {
}
}
Java
- 一次申请单个权限
PermissionUtils.INSTANCE.requestPermission(this, Manifest.permission.READ_PHONE_STATE, new PermissionListener() {
/**
* 权限允许时回调
*/
@Override
public void granted(String permission) {
}
/**
* 权限拒绝时回调
*/
@Override
public void denied(String permission) {
}
});
-
一次申请多个权限
PermissionUtils.INSTANCE.requestMultiplePermissions(this, Arrays.asList(Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS), new MultiplePermissionsListener() { /** * 多个权限全部被允许时回调 */ @Override public void allGranted() { } /** * 被拒绝的权限列表 */ @Override public void denied(List<String> list) { } });
希望TKPermission能帮助到有需要的同学们,愿大家工作顺利,天天开心!