这是我的服务:
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");
}
}
为了测试,我想要在通知到达时立即 关闭来自任何应用的所有通知。我已经在 onNotificationPosted 中调用了 Listener.this.cancelAllNotifications();
,但它没有起作用。
我做错了什么?我正在 Android 版本 5.0.2 上进行测试。
P.S.
我已经按照 Android 的说明添加了权限和意图过滤器。
<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>