Permision 权限申请封装,页面跳转封装
github地址
目前第一版封装了:页面启动、页面启动回调、获取通讯录联系人、获取相册图片、拍照获取图片、单条权限申请、多条权限申请,且已适配到Android 13
导入
maven { url 'https://jitpack.io' }
implementation 'com.github.GoodWeking:Permission:1.3'
权限申请
权限申请支持单条权限和多条权限申请,支持强制拒绝后弹窗提示用户并引导区设置也开启权限
//请求单条权限
launchPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) {
Toast.makeText(this, "是否成功:$it", Toast.LENGTH_SHORT).show()
}
//多条权限申请
launchPermission(arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.READ_SMS)) {
Toast.makeText(this, "是否成功:$it", Toast.LENGTH_SHORT).show()
}
默认强制拒绝弹窗提示
AlertDialog.Builder(context)
.setMessage(resources.getString(R.string.register_permission_dialog_msg))
.setPositiveButton(resources.getString(R.string.register_permission_dialog_confirm)) { _, _ ->
openSettingsPermission()
}
.setNeutralButton(
resources.getString(R.string.register_permission_dialog_cancel),
null
).show()
支持自定义
launchPermission(
permissions = arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.READ_SMS),
tipDialog = {
//自定义提示引导
AlertDialog.Builder(context)
.setMessage(resources.getString(R.string.register_permission_dialog_msg))
.setPositiveButton(resources.getString(R.string.register_permission_dialog_confirm)) { _, _ ->
openSettingsPermission()
}
.setNeutralButton(
resources.getString(R.string.register_permission_dialog_cancel),
null
).show()
}
) {
//权限申请结果
}
launchPermission(permissions = android.Manifest.permission.ACCESS_FINE_LOCATION,
tipDialog = {
//自定义提示
}) {
Toast.makeText(this, "是否成功:$it", Toast.LENGTH_SHORT).show()
}
跳转设置页
fun Context.openSettingsPermission() {
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:$packageName")
startActivity(this)
}
}
选择联系人
目前只封装了获取单个联系人以兼容到 api 33,在调用时请添加权限到清单文件 Manifest.permission.READ_CONTACTS
launchContact {
//it.name 联系人姓名
//it.phone 联系人电话
//it.saveDate 保存时间
//it.contactId 联系人id
}
Activity 跳转
launch<ContentActivity>("value1" to "111", "value2" to 222)
launchForResult<ContentActivity>("customer" to "mainActivity") { rsultCode: Int, data: Intent? ->
//回传值回调
}
获取图片
相册获取选择结果都是存放在sd的下载目录,需要再清单文件添加文件读写权限Android13以下Manifest.permission.WRITE_EXTERNAL_STORAGE
以上Manifest.permission.READ_MEDIA_IMAGES
path=Pair<String,String>
first:存放文件夹
second:文件名文章来源:https://www.toymoban.com/news/detail-595208.html
//相册获取
launchPic(path="camera" to ("${System.currentTimeMillis()}.jpg")) {
ivImage.setImageURI(it.uri)
}
相机获取需要再清单文件添加权限Android13以上Manifest.permission.READ_MEDIA_IMAGES
,Manifest.permission.CAMERA
以下Manifest.permission.WRITE_EXTERNAL_STORAGE
,Manifest.permission.CAMERA
文章来源地址https://www.toymoban.com/news/detail-595208.html
launchCamera(path="camera" to ("${System.currentTimeMillis()}.jpg")) {
//it.uri
//it.file 从uri转出的图片
}
到了这里,关于Android权限申请、相册选择图片、拍照、获取联系人、页面跳转的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!