我环顾四周,发现了一些使用新的 notificationlistenerservice 类的例子,但我遇到了问题,甚至没有听到通知发布事件。
我发现的例子是使用我的目标并不真正需要的 gui 和广播接收器。
我试图至少用这个来触发 toast 通知,但我什么也没得到。我已验证允许该应用程序收听通知并启用通知。
我有什么遗漏吗?
//listen for new notifications?
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Toast.makeText(getApplicationContext(), sbn.getPackageName().toString(), 1).show();
Toast.makeText(getApplicationContext(), "anything? anything? please?", 1).show();
}
//listen for the removal of the notities
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//uhh not done yet
}
//prepare
public void onCreate() {
super.onCreate();
//ongoing notification test, is this needed for notification listeners?
NotificationManager notificationManager;
Notification myNotification;
Context context = getApplicationContext();
myNotification = new Notification.Builder(context)
.setContentTitle("NotifyWake")
.setSmallIcon(R.drawable.ic_notification_open)
.setOngoing(true)
.build();
notificationManager = (NotificationManager)getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(1, myNotification);
//end ongoing notification
}