问题描述
这是我的服务:
公共类监听器扩展NotificationListenerService { 上下文语境; @覆盖 公共无效的onCreate(){ super.onCreate();
上下文= getApplicationContext(); } @覆盖 公共无效onNotificationPosted(StatusBarNotification SBN){
串包= sbn.getPackageName();
串股票= sbn.getNotification()tickerText.toString()。
捆绑额外= sbn.getNotification()临时演员。
字符串title = extras.getString(android.title);
字符串文本= extras.getCharSequence(android.text)的toString()。 Log.i(味精,包);
Log.i(味精,股票代码);
Log.i(味精,称号);
Log.i(味精,文本); 意图msgrcv =新意图(MSG);
msgrcv.putExtra(包,包);
msgrcv.putExtra(股票,股票代码);
msgrcv.putExtra(题,题);
msgrcv.putExtra(文字,文本); LocalBroadcastManager.getInstance(上下文).sendBroadcast(msgrcv);
Toast.makeText(背景下,有东西,Toast.LENGTH_LONG).show(); //最后弦乐T = sbn.getKey(); Listener.this.cancelAllNotifications();
} @覆盖 公共无效onNotificationRemoved(StatusBarNotification SBN){
Log.i(味精,通知删除); }
}
有关测试,我想为关闭所有通知从任何应用,尽快为他们到达。我已经叫 Listener.this.cancelAllNotifications();
里面onNotificationPosted,但它不工作。
我是什么做错了吗?我在Android 5.0.2版本测试这一点。
P.S。
我添加的权限与意图过滤器以来,由于Android提到了。
<服务机器人:名字=的NotificationListener
机器人:标签=@字符串/服务名
机器人:权限=android.permission.BIND_NOTIFICATION_LISTENER_SERVICE>
&所述;意图滤光器>
<作用机器人:名字=android.service.notification.NotificationListenerService/>
&所述; /意图滤光器>
< /服务>
确保通知访问被激活(选中)的应用程序实现您的 NotificationListenerService
可以在设置菜单中进行验证。这是在任取决于Android版本的地址。为Android 5.0,它应该是在:
设置 - >声音和放大器;通知 - >通知访问
否则,您可能会发现在:
设置 - >安全 - >通知访问
试着启用它为您的应用程序,然后尝试清除通知。
This is my service:
public class Listener extends NotificationListenerService {
Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String pack = sbn.getPackageName();
String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");
String text = extras.getCharSequence("android.text").toString();
Log.i("Msg",pack);
Log.i("Msg",ticker);
Log.i("Msg",title);
Log.i("Msg",text);
Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_LONG).show();
//final String T= sbn.getKey();
Listener.this.cancelAllNotifications();
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}
For testing, I would like to DISMISS ALL NOTIFICATIONS FROM ANY APP as soon as they arrive. I have called Listener.this.cancelAllNotifications();
inside onNotificationPosted, but it does not work.
What am I doing wrong ? I am testing this on android version 5.0.2.
P.S.
I HAVE added the permissions along with the intent filter, as android mentions.
<service android:name=".NotificationListener"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
Make sure the 'Notification Access' is active (checked) for the app implementing your NotificationListenerService.
It can be verified in 'Settings' menu. It is at either of the addresses depending upon the Android version. For Android 5.0, it should be at:Settings -> Sound & notification -> Notification Access
Or else, you may find at:Settings -> Security -> Notification Access
Try enabling it for your app and then try to clear the notifications.
这篇关于不能使用NotificationListenerService取消通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!