gpt4 book ai didi

android - 在使用权限时减少编写相同无聊代码的方法

转载 作者:行者123 更新时间:2023-11-29 23:01:39 25 4
gpt4 key购买 nike

目前,我正在为 Android 编写一个聊天应用程序。从 23 SDK 及以上开始,它需要一些用户必须允许的权限,例如非常重要(我的聊天将使用创建特定聊天的位置)和一些小功能,例如将图像上传到 Firebase 存储(它需要访问手机存储空间,因此需要适当的许可)。

我有以下回调接口(interface)。

object PermissionUtils {

interface PermissionAskListener {


fun onPermissionGranted()
/*
User has already granted this permission
The app must had been launched earlier and the user must had "allowed" that permission
*/


fun onPermissionRequest()
/*
The app is launched FIRST TIME..
We don't need to show additional dialog, we just request for the permission..

*/


fun onPermissionPreviouslyDenied()
/*
The app was launched earlier and the user simply "denied" the permission..
The user had NOT clicked "DO NOT SHOW AGAIN"
We need to show additional dialog in this case explaining how "allowing this permission" would be useful to the user
*/


fun onPermissionDisabled()
/*
The app had launched earlier and the user "denied" the permission..
AND ALSO had clicked "DO NOT ASK AGAIN"
We need to show Toask/alertdialog/.. to indicate that the user had denied the permission by checking do not disturb too...
So, you might want to take the user to setting>app>permission page where the user can allow the permission..


*/

}

fun checkForPermission(activity: Activity, permission: String, permissionAskListener: PermissionAskListener) {
//code omitted, here's the logic of calls listener members
}
}

而且,我在这样的代码中使用它:

//calling from onCreate()
checkForPermission(
this, android.Manifest.permission.READ_EXTERNAL_STORAGE,
object : PermissionAskListener {
override fun onPermissionGranted() {
showToast(getString(R.string.msg_permissions_granted), Toast.LENGTH_LONG)
uplodAnImageToFirebase()

}

override fun onPermissionRequest() {
ActivityCompat.requestPermissions(
this@MainActivity, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE), readStorage
)
}

override fun onPermissionPreviouslyDenied() {
AlertDialog.Builder(this@MainActivity)
.setTitle(getString(R.string.title_permission_required))
.setMessage(getString(R.string.msg_permission_required))
.setCancelable(false)
.setPositiveButton(getString(R.string.action_allow)) { _, _ ->
ActivityCompat.requestPermissions(
this@MainActivity,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
readStorage
)
}
.setNegativeButton(getString(R.string.action_cancel)) { dialog, _ ->
dialog.cancel()
showToast(getString(R.string.msg_we_cant_give_functionality), Toast.LENGTH_LONG)
}
.show()
}

override fun onPermissionDisabled() {
AlertDialog.Builder(this@MainActivity)
.setTitle(getString(R.string.title_permission_disabled))
.setMessage(getString(R.string.msg_please_enable_permission))
.setCancelable(false)
.setPositiveButton(
getString(R.string.action_go_to_settings)
) { _, _ -> startActivity(Intent(Settings.ACTION_SETTINGS)) }
.setNegativeButton(getString(R.string.action_cancel)) { dialog, _ ->
dialog.cancel()
showToast(getString(R.string.msg_we_cant_give_functionality), Toast.LENGTH_LONG)
}
.show()
}
}
)

正如你从代码中看到的,只有 onPermissionGranted() 做一些特别的事情,而 onPermissionPreviouslyDenied() 和 onPermissionDisabled() 只是告诉用户无聊和常见的事情,我想封装到一些类中,这些类将为额外重要的事情(比如位置;如果权限被拒绝,我想关闭整个应用程序),或者只是上传,这只会阻止功能。

我知道如何请求许可和其他类似的东西。我不知道如何使用对话框创建这些类 - 创建枚举,每当我从 Activity 中调用 onDisabled/onPreviouslyDenied 方法时,我都会传递它,或者为它创建 Builder,或者创建 Factory ... 如果你 TL;DR 案例,那么就回答:“如何在我的案例中减少相同的代码?”

最佳答案

另一种解决方案是创建一个 BaseActivity 类,并让应用程序中的其他 Activity 成为 BaseActivity 的子类。

有点像..

class BaseActivity: AppCompatActivity {
override fun onCreate() {
super.onCreate()
checkForPermissions() // do your permission check code
}
}

class MainActivity: BaseActivity {
override fun onCreate() {
super.onCreate() // calls BaseAcivitiy's onCreate, which triggers the checkForPermissions
}
}

关于android - 在使用权限时减少编写相同无聊代码的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56903101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com