【问题标题】:How can I listen for when a notification occurs for my app and calendar app?当我的应用程序和日历应用程序发生通知时,如何监听?
【发布时间】:2013-02-19 05:12:06
【问题描述】:

我希望能够监听(BroadcastReceiver?)每当我的应用程序和日历应用程序发生通知时,即使用户没有与之交互,即使屏幕关闭。

我还希望能够知道用户何时滑开通知。

我知道AccessibilityEvent 可以执行第一件事(侦听通知),但不能执行第二件事(Cannot listen for notification dismissal)。还有其他方法吗?

我该怎么做?当我自己的通知发生(它们用于日历事件)并且知道通知仍在通知托盘中时,我至少可以调用我的应用程序/侦听器吗?

【问题讨论】:

标签: android android-intent broadcastreceiver android-notifications


【解决方案1】:

您可以收听所有日历通知信息。你怎么说,你应该使用 AccessibilityService 并切换通知类型。然后,如果它来自谷歌日历,您可以比较包名称。

    public void onAccessibilityEvent(AccessibilityEvent event) {

    try {

        switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_VIEW_CLICKED:

            Log.d(LOG_SERVICE, "Click");

            break;
        case AccessibilityEvent.TYPE_VIEW_FOCUSED:

            Log.d(LOG_SERVICE, "Focused");

            break;
        case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:

            Log.d(LOG_SERVICE, "Ihas been received a not" + type);

            String pack = (String) event.getPackageName();
                            if (pack.equalsIgnoreCase("com.google.android.calendar")) {
                   //The event information.
                    }
            break;

        default:
            type = "otro";
            Log.d(LOG_SERVICE, "otro");

            break;
        }

        AccessibilityNodeInfo notinfo = event.getSource();
        if (notinfo != null) {
            doSomethingWithNodeInfo(notinfo);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

如果 Android 设备带有 ICS 或更多。如果应用开发者设计的通知具有可访问性,您可以使用 AccessibilityNodeInfo。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2016-11-20
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多