如何使其仅在 Kotlin 中允许通知访问时才进入主屏幕

问题描述 投票:0回答:1

我希望应用程序像这样运行:

当您运行应用程序时,您将进入窗口设置通知访问权限,如果您在允许访问应用程序的情况下返回,则会转到主屏幕。当未授予通知访问权限时,您将进入通知访问窗口,直到授予权限。

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val notificationManager =
            this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        if (Build.VERSION.SDK_INT >= 23) {
            if (!notificationManager.isNotificationPolicyAccessGranted) {
                this.startActivity(Intent(android.provider.Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
            }
        }
    }

代码如上编写,但是无论是否允许通知权限,如果返回都会被带到主页面。

如何修改我的代码,以便只有在获得许可的情况下才能导航到主页?

kotlin android-notifications
1个回答
0
投票

在 MainActivity.kt 中使用此代码

if (!isNotificationListenerEnabled(this, WhatsAppService::class.java)) { val enableNotificationListenerIntent = Intent(ACTION_NOTIFICATION_LISTENER_SETTINGS) 启动活动(启用NotificationListenerIntent) }

private fun isNotificationListenerEnabled(上下文:Context,listenerService:Class<*>):Boolean { val flat = Settings.Secure.getString( 上下文.contentResolver, “启用通知监听器” ) return flat?.contains(listenerService.name) == true }

© www.soinside.com 2019 - 2024. All rights reserved.