gpt4 book ai didi

android - 如何以编程方式检查 "enabled_notification_listeners"?

转载 作者:行者123 更新时间:2023-12-04 13:03:26 25 4
gpt4 key购买 nike

我需要检查我的应用程序的服务是否在 中启用或未启用“通知访问” .
我正在使用此代码来检查这一点。代码在 上运行良好牛轧糖 ,但它在 上崩溃棉花糖 .如何在 上做到这一点?棉花糖 ?

android.provider.Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName())
这是 LogCat 中的错误 :
12-06 15:28:02.905 4428-4428/com.inetsoho.app.SkyChat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.inetsoho.app.SkyChat, PID: 4428
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4740)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at com.inetsoho.app.SkyChat.ui.activities.authorization.LandingActivity.SignUpPhoneNumberOnClickListener(LandingActivity.java:110)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4735)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

使用这个函数来获取我们的包是否有通知监听器。此函数将返回 True 或 false。

boolean hasNotificationAccess()
{
ContentResolver contentResolver = this.getContentResolver();
String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = this.getPackageName();

// check to see if the enabledNotificationListeners String contains our package name
return !(enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName));
}

关于android - 如何以编程方式检查 "enabled_notification_listeners"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673127/

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