发布网友 发布时间:2022-04-22 05:43
共1个回答
热心网友 时间:2022-04-11 23:16
利用Android辅助工具类AccessibilityService可以获取手机当前页面的信息,用户的操作事件等等.首先新建一个类继承AccessibilityService:[html] view plain copy
<span style="font-family:Times New Roman;font-size:14px;">public class MyAccessibilityService extends AccessibilityService {
private final String TAG = "MyAccessibilityService";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
int eventType = event.getEventType();
String eventTypeName = "";
switch (eventType) {
case AccessibilityEvent.TYPE_VIEW_CLICKED:
eventTypeName = "TYPE_VIEW_CLICKED";
break;
case AccessibilityEvent.TYPE_VIEW_FOCUSED:
eventTypeName = "TYPE_VIEW_FOCUSED";
break;
case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
eventTypeName = "TYPE_VIEW_LONG_CLICKED";
break;
case AccessibilityEvent.TYPE_VIEW_SELECTED:
eventTypeName = "TYPE_VIEW_SELECTED";
break;
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
eventTypeName = "TYPE_VIEW_TEXT_CHANGED";
break;
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
eventTypeName = "TYPE_WINDOW_STATE_CHANGED";
break;
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
eventTypeName = "TYPE_NOTIFICATION_STATE_CHANGED";
break;
case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_END:
eventTypeName = "TYPE_TOUCH_EXPLORATION_GESTURE_END";
break;
case AccessibilityEvent.TYPE_ANNOUNCEMENT:
eventTypeName = "TYPE_ANNOUNCEMENT";
break;
case AccessibilityEvent.TYPE_TOUCH_EXPLORATION_GESTURE_START:
eventTypeName = "TYPE_TOUCH_EXPLORATION_GESTURE_START";
break;
case AccessibilityEvent.TYPE_VIEW_HOVER_ENTER:
eventTypeName = "TYPE_VIEW_HOVER_ENTER";
break;
case AccessibilityEvent.TYPE_VIEW_HOVER_EXIT:
eventTypeName = "TYPE_VIEW_HOVER_EXIT";
break;
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
eventTypeName = "TYPE_VIEW_SCROLLED";
break;
case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
eventTypeName = "TYPE_VIEW_TEXT_SELECTION_CHANGED";
break;
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
eventTypeName = "TYPE_WINDOW_CONTENT_CHANGED";
break;
}
Log.i(TAG, "eventType:" + eventType);
Log.i(TAG, "eventTypeName:" + eventTypeName);
}
@Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
}</span>
然后新建一个xml配置文件来配置我们的MyAccessibility类:accessibilityservice.xml :
[html] view plain copy
<span style="font-family:Times New Roman;font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"
android:description="@string/app_name"
android:settingsActivity="com.example.xxx.android.accessibility.ServiceSettingsActivity" /></span>
android:accessibilityEventTypes="typeAllMask",相应所有的事件,就是MyAccessibility类中swich中的那些事件.android:accessibilityFeedbackType="feedbackGeneric",手机用什么方式将信息反馈给用户,语音震动等等,这里默认;
android:canRetrieveWindowContent="true",允许获取手机页面中的信息
android:settingsActivity中com.example.xxx是你的应用的包名
然后将这个xml配置文件配置到项目中:
[html] view plain copy
<span style="font-family:Times New Roman;font-size:14px;"> <service
android:name="com.example.xxx.MyAccessibilityService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibilityservice" />
</service></span>
然后在我们手机的设置中的辅助功能里,打开这个辅助服务就可以了.利用这个类我们可以获取当前手机页面的信息,包括控件ID,控件值;当前类的名称以及用户的一些操作和手机状态的变化. 所以利用这些可以做手机自动安装软件,模拟点击,手机自动滑屏等.
public void onAccessibilityEvent(AccessibilityEvent event){}
通过这个event我们可以得到:
event.getEventType()-当前事件的类型; event.getClassName()-当前类的名称; event.getSource()-当前页面中的节点信息,等等.更详细信查看官方API
遍历获取窗口中所有的节点信息也可以用这个方法:
[html] view plain copy
<span style="font-family:Times New Roman;font-size:14px;">public void getInfo() {
AccessibilityNodeInfo info = getRootInActiveWindow();
if (info != null) {
if (info.getChildCount() == 0) {
Log.i(TAG, "控件名称:" + info.getClassName());
Log.i(TAG, "控件中的值:" + info.getText());
Log.i(TAG, "控件的ID:" + info.getViewIdResourceName());
Log.i(TAG, "点击是否出现弹窗:" + info.canOpenPopup());
} else {
for (int i = 0; i < info.getChildCount(); i++) {
if (info.getChild(i) != null) {
recycle(info.getChild(i));
}
}
}
}
}</span>
不过这个方法对当前的SDK版本有一定的要求,需要API level 19及以上.