以编程方式单击通知 [英] Click on the Notification programmatically

查看:112
本文介绍了以编程方式单击通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在收到通知后单击该通知.

I trying click on the notification after receiving it.

我可以使用辅助功能"服务拖动通知抽屉.

I'm able to drag the notification drawer using the Accessibility service.

要单击我正在使用的accessibilityEvent.getSource()通知, accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);

For clicking the notification I'm using accessibilityEvent.getSource() and accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);

我的代码:

public class MyAccessibilityService extends AccessibilityService {

/**
 * On receiving the AccessibilityEvent performs the Actions
 *
 * @param event
 */
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.i(TAG, "Get the Accessibility Event");
    if (event.getEventType() == AccessibilityEvent.TYPE_TOUCH_INTERACTION_END) {
        handleTypeTouchInteractionEndEvents(event);
    }
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        handleTypeWindowStateChangedEvents(event);
    }
}

@Override
public void onServiceConnected() {
    AccessibilityServiceInfo accessibilityServiceInfo = new AccessibilityServiceInfo();
    accessibilityServiceInfo.eventTypes = AccessibilityEvent.TYPE_TOUCH_INTERACTION_END | AccessibilityEvent.TYPE_VIEW_CLICKED |
            AccessibilityEvent.TYPE_VIEW_FOCUSED | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
    accessibilityServiceInfo.feedbackType = AccessibilityServiceInfo.CAPABILITY_CAN_PERFORM_GESTURES;
    accessibilityServiceInfo.flags = AccessibilityServiceInfo.DEFAULT | AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS;
    accessibilityServiceInfo.notificationTimeout = NOTIFICATION_TIME_OUT;
    this.setServiceInfo(accessibilityServiceInfo);
}

@Override
public void onInterrupt() {
    //Do Nothing
}

/**
 * Performs the action for TYPE_TOUCH_INTERACTION_END events
 *
 * @param accessibilityEvent
 */
private void handleTypeTouchInteractionEndEvents(AccessibilityEvent accessibilityEvent) {
    switch (accessibilityEvent.getAction()) {
        case EXPAND_NOTIFICATIONS_DRAWER:
            Log.i(TAG, "perfroming expand notification bar");         performGlobalAction(AccessibilityService.GLOBAL_ACTION_NOTIFICATIONS);
            break;
        default:
            Log.i(TAG, "Event type not defined");
    }
}

private void handleTypeWindowStateChangedEvents(AccessibilityEvent accessibilityEvent) {
    switch (accessibilityEvent.getAction()) {
        case ACTION_CLICK:
            Log.i(TAG, "Performing click Action");
            findNotificationIconAndClick("com.my.app:id/notification_icon", accessibilityEvent);
            Log.i(TAG, "Click Action is successfull");
        default:
            Log.i(TAG, "Event type not defined");
    }
}


public void findNotificationIconAndClick(String id, AccessibilityEvent accessibilityEvent) {
    AccessibilityNodeInfo accessibilityNodeInfo = accessibilityEvent.getSource();
    List<AccessibilityNodeInfo> nodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(id);
    if (nodeInfoList != null && !nodeInfoList.isEmpty()) {
        for (AccessibilityNodeInfo nodeInfo : nodeInfoList) {
            if (nodeInfo != null) {
                performClick(nodeInfo);
                break;
            }
        }
    }
}
}

我是使用辅助功能服务的新手.有人可以告诉我我是否有任何错误,或者使用辅助功能无法单击通知吗?如果不使用辅助功能服务单击通知,还有其他可能性吗?

I'm new to using accessibility service. Can someone tell me if there is any mistake from my side or if it is not possible to click on a notification using accessibility service? Are there any other possibilities without using Accessibility Service to click on the Notification?

推荐答案

如果要单击通知,则必须扩展NotificationListenerService,实现必须实现的内容,然后可以调用sbn.getNotification().contentIntent.send().这种方式就像用户单击通知托盘中的通知一样.

If you want to click on the notification, you have to extend the NotificationListenerService, implement what have to be implemented then you can call sbn.getNotification().contentIntent.send(). This way is like if user was clicking on the notification from the notification tray.

这篇关于以编程方式单击通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆