为什么getActiveNotifications()返回Null?怎么了? [英] Why does getActiveNotifications() return Null? Whats wrong?

查看:389
本文介绍了为什么getActiveNotifications()返回Null?怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在Android中学习通知,并且想知道为什么这段代码无效......而不是它的返回Null?



这段代码片段来自我的MainActivity,

NotificationService是我的公共类,它扩展了NotificationListenerService

i还在Manifest中为我的应用程序发出了必要的权限来观看通知!





 受保护  void  onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

NotificationService ns = new NotificationService();
StatusBarNotification [] sbn = ns.getActiveNotifications();

if (sbn!= null)
{
Log.i( XXXXXX 长度: + sbn.length);
}
else
{
Log.i( XXXXXX Null );
}

}





我想把NotificationListenerService里面的getActiveNotifications()转到在我启动我的应用程序之前,返回状态栏中已经存在的通知数组...所以在我启动应用程序并且OnCreate被解雇后...即使有4个当前通知我仍然会得到Null?

解决方案

https:// www。 google.com/search?q=getActiveNotifications [ ^ ]。


请确保您在通知访问设置窗格中启用了您的应用您的Android设备。



设置 - >中的通知访问部分启用您的应用程序安全

hi guys,
was learning notifications in Android and was wonderring why was this code not working... instead its returning Null?

This code snippet is from my MainActivity ,
NotificationService is my public class that extends NotificationListenerService
i have also made the necessary permissions in the Manifest for my app to watch notifications!


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

NotificationService ns = new NotificationService();
StatusBarNotification[] sbn = ns.getActiveNotifications();
      
        if (sbn != null)
        	{
        	Log.i("XXXXXX", "Lenght: " + sbn.length);
        	}
        else
        	{
        	Log.i("XXXXXX", "Null" );
        	}
       
    }



I am trying to get the getActiveNotifications() inside the NotificationListenerService to return me an array of the Notifications that are already present in the statusbar before i had launched my app... so after i have launched the app and the OnCreate is fired... i still get Null even though there are 4 current notifications?

解决方案

A number of suggestions at https://www.google.com/search?q=getActiveNotifications[^].


Please make sure you have enabled your app in the Notification Access settings pane of your Android device.

Enable your application in Notification Access section from Settings -> Security


这篇关于为什么getActiveNotifications()返回Null?怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆