点击程序的通知点击程序的通知(Click on the Notification programma

2019-05-12 06:54发布

我收到后试穿通知点击。

我能够拖动使用辅助服务的通知抽屉。

对于点击我使用的通知accessibilityEvent.getSource()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;
            }
        }
    }
}
}

我是新来使用辅助服务。 谁能告诉我,如果有任何错误,从我的身边,或者如果它是不可能的点击使用无障碍服务的通知? 有没有不使用辅助服务,点击通知任何其他的可能性?

Answer 1:

并非所有的可访问性事件将返回源。 事实上,大部分(或者至少是最经常发生的事件)没有。 确保您限制事件的合理子集的配置和/或做一个空检查event.getSource()



Answer 2:

如果你想点击的通知,您必须扩展NotificationListenerService,实现什么都可以实现,那么你可以调用sbn.getNotification().contentIntent.send() 这种方式是一样,如果用户点击了从通知栏通知。



文章来源: Click on the Notification programmatically