package com.example.test01; import java.util.List; import android.R.bool; import android.accessibilityservice.AccessibilityService; import android.accessibilityservice.AccessibilityServiceInfo; import android.app.Notification; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; public class MyAccessibilityService extends AccessibilityService { @Override public void onAccessibilityEvent(AccessibilityEvent arg0) { int eventyp = arg0.getEventType(); if (eventyp == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) { List<CharSequence> texts = arg0.getText(); if (texts != null) { for (CharSequence charSequence : texts) { //获取通知栏的标题 String string = String.valueOf(charSequence); if (string.contains("快抢单")) { if (arg0.getParcelableData() == null || !(arg0.getParcelableData() instanceof Notification)) { return; } //模拟点击通知栏直接跳入Activity Notification notification = (Notification) arg0.getParcelableData(); PendingIntent pendingIntent = notification.contentIntent; try { pendingIntent.send(); } catch (CanceledException e) { e.printStackTrace(); } } } } } else if (eventyp == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { if ("com.homelink.ui.app.arrange.BookShowActivity".equals(arg0.getClassName())) { if (getRootInActiveWindow() == null) { return; } boolean isEnd=true; List<AccessibilityNodeInfo> list=null; while (isEnd) { list = getRootInActiveWindow().findAccessibilityNodeInfosByText("赶紧抢"); if (list.size()>0) { isEnd=false; } } for(int i=0;i<list.size();i++){ AccessibilityNodeInfo parent = list.get(i); if (parent.getClassName().equals("android.widget.Button")&&parent.isEnabled()) { parent.performAction(AccessibilityNodeInfo.ACTION_CLICK); } } } } } @Override public void onInterrupt() { // TODO Auto-generated method stub } @Override protected void onServiceConnected() { // TODO Auto-generated method stub super.onServiceConnected(); AccessibilityServiceInfo info = getServiceInfo(); info.notificationTimeout = 100; info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN; info.packageNames = new String[] { "com.homelink.im" }; setServiceInfo(info); } }