我想在使用AccessibilityService的
android设置中单击按钮,就像greenify一样,但我找不到具体的按钮.请帮我.
MyAccessibilityService .java:
public class MyAccessibilityService extends AccessibilityService { private static final String TAG = MyAccessibilityService.class .getSimpleName(); @Override public void onAccessibilityEvent(AccessibilityEvent event) { Log.i(TAG,"ACC::onAccessibilityEvent: " + event.getEventType()); //TYPE_WINDOW_STATE_CHANGED = 32,if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) { AccessibilityNodeInfo nodeInfo = event.getSource(); Log.i(TAG,"ACC::onAccessibilityEvent: nodeInfo=" + nodeInfo.getText()); List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button"); for (AccessibilityNodeInfo node : list) { Log.i(TAG,"ACC::onAccessibilityEvent: " + event.getEventType() + " " + node); }
编辑:
只有当type为TYPE_WINDOW_STATE_CHANGED时,我才能得到nodeInfo对象.
解决方法
打开一个应用程序的Appinfo,启用强制关闭按钮进行测试:
public class MyAccessibilityService extends AccessibilityService { private static final String TAG = MyAccessibilityService.class .getSimpleName(); @Override public void onAccessibilityEvent(AccessibilityEvent event) { Log.i(TAG,"ACC::onAccessibilityEvent: " + event.getEventType()); //TYPE_WINDOW_STATE_CHANGED == 32 if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event .getEventType()) { AccessibilityNodeInfo nodeInfo = event.getSource(); Log.i(TAG,"ACC::onAccessibilityEvent: nodeInfo=" + nodeInfo); if (nodeInfo == null) { return; } List<AccessibilityNodeInfo> list = nodeInfo .findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button"); for (AccessibilityNodeInfo node : list) { Log.i(TAG,"ACC::onAccessibilityEvent: left_button " + node); node.performAction(AccessibilityNodeInfo.ACTION_CLICK); } list = nodeInfo .findAccessibilityNodeInfosByViewId("android:id/button1"); for (AccessibilityNodeInfo node : list) { Log.i(TAG,"ACC::onAccessibilityEvent: button1 " + node); node.performAction(AccessibilityNodeInfo.ACTION_CLICK); } } } @Override public void onServiceConnected() { Log.i(TAG,"ACC::onServiceConnected: "); } @Override public void onInterrupt() { // TODO Auto-generated method stub } }