[英]Android Compose: how to get notification permission
我需要獲得許可才能打開或關閉請勿打擾模式。 通常,在不編寫的情況下,我會使用以下代碼並檢查已啟動活動的結果:
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (!mNotificationManager.isNotificationPolicyAccessGranted) {
val intentNotifica = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
startActivity(intentNotifica)
}
但是使用jetpack compose,我不知道如何檢查用戶是否已獲得權限。 例如,使用以下代碼,我無法知道用戶按下按鈕后,是否已獲得權限:
fun DoNotDisturbPermission() {
val context = LocalContext.current
val hasPermission = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (!hasPermission.isNotificationPolicyAccessGranted) {
Button(onClick = {
val intentNotifica = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
context.startActivity(intentNotifica)
}) {
Text("get permission")
}
} else {
Text("Already granted")
}}
另外,我嘗試使用帶有rememberPermissionState(Manifest.permission.ACCESS_NOTIFICATION_POLICY)
的伴奏庫,但它不能正常工作。
方法是一樣的。 像這樣創建一個函數
fun hasPermissions(context: Context) = PERMISSIONS_REQUIRED.all {
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
然后把這個塊放在你的可組合中
if (!hasPermissions(context)) {
requestPermissions(
context as Activity,
PERMISSIONS_REQUIRED,
PERMISSIONS_REQUEST_CODE
)
}
檢查使用 rememberPermissionState APIs..您使用的方法是正確的。
這是文檔中的一個片段
@Composable
private fun FeatureThatRequiresCameraPermission() {
// Camera permission state
val cameraPermissionState = rememberPermissionState(
android.Manifest.permission.CAMERA
)
when (cameraPermissionState.status) {
// If the camera permission is granted, then show screen with the feature enabled
PermissionStatus.Granted -> {
Text("Camera permission Granted")
}
is PermissionStatus.Denied -> {
Column {
val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
// If the user has denied the permission but the rationale can be shown,
// then gently explain why the app requires this permission
"The camera is important for this app. Please grant the permission."
} else {
// If it's the first time the user lands on this feature, or the user
// doesn't want to be asked again for this permission, explain that the
// permission is required
"Camera permission required for this feature to be available. " +
"Please grant the permission"
}
Text(textToShow)
Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
Text("Request permission")
}
}
}
}
}
我就是這樣做的。 我在 Utils package 的權限 package 中有以下文件。
權限檢查器:
@Composable
fun checkNotificationPolicyAccess(
notificationManager: NotificationManager,
context: Context
): Boolean {
if (notificationManager.isNotificationPolicyAccessGranted) {
return true
} else {
PermissionDialog(context)
}
return false
}
權限對話框:
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PermissionDialog(context: Context) {
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
properties = DialogProperties(
usePlatformDefaultWidth = true
),
title = { Text("Permission Needed") },
text = {
Text("Allow ******* to access Do Not Disturb Settings? Pressing 'No' will close the app as it cannot work without access. Thanks.")
},
onDismissRequest = { openDialog.value = true },
dismissButton = {
Button(
onClick = {
exitProcess(0)
},
colors = ButtonDefaults.buttonColors(Color(0xffFF9800))
) {
Text(text = "No")
}
},
confirmButton = {
Button(
onClick = {
openDialog.value = false
val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
startActivity(context, intent, null)
},
colors = ButtonDefaults.buttonColors(Color(0xffFF9800))
) {
Text(text = "Yes")
}
}
)
}
}
然后我的主要活動如下所示:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
setContent {
MyTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
checkNotificationPolicyAccess(notificationManager, this)
HomePage()
}
}
}
}
}
相反,如果您獲得 root 訪問權限然后阻止或啟用,那么您將擁有對通知的完全訪問權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.