1

我环顾四周,发现了一些使用新的 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

}
4

1 回答 1

0

想通了,您必须创建一些线程或广播方法来从发布/删除事件中卸载任何内容。像 toast 这样的东西不起作用,因为该方法会触发线程错误并静默失败(在 logcats 中注明)

因此,一个简单的解决方案是扩展广播接收器并将繁重的工作和 gui 传递给该方法。

于 2013-08-10T03:57:00.693 回答