【问题标题】:Android execute a notification action with NotificationListenerServiceAndroid 使用 NotificationListenerService 执行通知操作
【发布时间】:2020-10-31 21:02:06
【问题描述】:

我有一个 NotificationListenerService 类,它可以读取系统中的活动通知。

所有必要的权限都已设置,一切都按预期进行。

一个简单的问题出现在我身上。

有一个带有操作的通知:

如何以编程方式“按下”这些按钮?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 不幸的是我还没有找到任何东西:/

标签: android android-notifications


【解决方案1】:

我忘了检查Notification 的字段,其中定义了所有Notification.Actions。

所以你可以得到他们的actionIntent 并简单地发送它:

public static class MyNotificationListener extends NotificationListenerService {

    public MyNotificationListener() {
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        try {
          sbn.getNotification().actions[0].actionIntent.send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }
}

想法来自这里:How to cancel an ongoing notification of another app?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多