Android AccessibilityService无障碍服务(二)

  1. 当服务未开启时,快速的跳转到开启服务的界面。
if (!OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(this,
            AccessibilitySampleService.class.getName())){// 判断服务是否开启
          OpenAccessibilitySettingHelper.jumpToSettingPage(this);// 跳转到开启页面
        }else {
          Toast.makeText(this, "服务已开启", Toast.LENGTH_SHORT).show();
        }

用到的方法具体实现:

/**
 * 开启无障碍服务帮助类
 * Created by mazaiting on 2017/8/18.
 */
public class OpenAccessibilitySettingHelper {

  /**
   * 跳转到无障碍服务设置页面
   * @param context 设备上下文
   */
  public static void jumpToSettingPage(Context context){
    Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  /**
   * 判断是否有辅助功能权限
   * @return true 已开启
   *          false 未开启
   */
  public static boolean isAccessibilitySettingsOn(Context context,String className){
    if (context == null){
      return false;
    }
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningServiceInfo> runningServices =
        activityManager.getRunningServices(100);// 获取正在运行的服务列表
    if (runningServices.size()<0){
      return false;
    }
    for (int i=0;i<runningServices.size();i++){
      ComponentName service = runningServices.get(i).service;
      if (service.getClassName().equals(className)){
        return true;
      }
    }
    return false;
  }
}

2.模拟点击,创建模拟点击的Activity为AccessibilityNormalSampleActivity,并在AndroidManifest.xml将AccessibilityNormalSampleActivity与AccessibilitySampleService配置在同一个进程,若不在同一进程,则获取到的AccessibilityService与AccessibilityEvent为空。

android:process=":BackgroundService"

配置文件为:

<!-- 注册辅助功能服务 -->
    <service
        android:name=".service.AccessibilitySampleService"
        android:enabled="true"
        android:exported="true"
        android:label="@string/accessibility_tip"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:process=":BackgroundService">

      <!-- android:label="@string/accessibility_tip" 在设置中显示的文字 -->
      <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
      </intent-filter>
      <!-- 通过xml文件完成辅助功能相关配置,也可以在onServiceConnected中动态配置 -->
      <meta-data
          android:name="android.accessibilityservice"
          android:resource="@xml/accessibility_config" />
    </service>

    <activity android:name=".ui.AccessibilityNormalSampleActivity"
        android:process=":BackgroundService"></activity>

AccessibilityNormalSampleActivity界面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_accessibility_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

  <CheckBox
      android:id="@+id/normal_sample_checkbox"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="复选框开关"/>

  <RadioButton
      android:id="@+id/normal_sample_radiobutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:text="单选按钮"/>

  <ToggleButton
      android:id="@+id/normal_sample_togglebutton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"/>

  <Button
      android:id="@+id/normal_sample_back"
      android:layout_marginTop="20dp"
      android:text="退出本页面"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
</LinearLayout>

3.创建一个单例类来控制模拟点击AccessibilityOperator。

/**
 * 控制无障碍服务
 * Created by mazaiting on 2017/8/18.
 */
public class AccessibilityOperator {
  private static final String TAG = "AccessibilityOperator";
  private static AccessibilityOperator mInstance;
  private AccessibilityOperator(){}
  public static AccessibilityOperator getInstance() {
    if (mInstance == null){
      synchronized (AccessibilityOperator.class){
        if (mInstance == null){
          mInstance = new AccessibilityOperator();
        }
      }
    }
    return mInstance;
  }
}
  1. 创建一个用来模拟点击的界面
    在要点击的Activity中创建一个Handler来执行延时消息,创建AccessibilityOperator对象,在onCreate方法中获取单例对象。
  private Handler mHandler = new Handler(Looper.getMainLooper());
  private AccessibilityOperator accessibilityOperator;

  @Override protected void onCreate(Bundle savedInstanceState) {
      .....// 省略布局填充
      accessibilityOperator = AccessibilityOperator.getInstance();
    }

并在onResume方法中进行模拟点击。此处只是调用AccessibilityOperator中的方法,因此直接贴代码:

@Override protected void onResume() {
    super.onResume();
    // 执行延时任务
    clickText();
  }

  /**
   * 按文本点击
   */
  private void clickText() {
    clickTextItem("复选框",1);
    clickTextItem("单选按钮",2);
    clickTextItem("关闭",3);
    clickTextItem("退出本页面",4);
  }

/**
   * 文本单个延时点击
   * @param text 文本内容
   * @param num 延时倍数
   */
  private void clickTextItem(final String text,int num) {
    mHandler.postDelayed(new Runnable() {
      @Override public void run() {
        final boolean isSuccess = accessibilityOperator.clickText(text);
        runOnUiThread(new Runnable() {
          @Override public void run() {
            popToast(isSuccess, text);
          }
        });
      }
    },2000*num);
  }

  /**
   * 弹出吐司
   * @param isSuccess
   * @param msg
   */
  private void popToast(boolean isSuccess, String msg) {
    if (isSuccess) {
      Toast.makeText(this, msg + "点击成功", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(this, msg + "点击失败", Toast.LENGTH_SHORT).show();
    }
  }
  1. 在accessibilityOperator.clickText(text)文本时,系统会先调用AccessibilitySampleService中的onAccessibilityEvent方法,因此在AccessibilityOperator创建一个updateEvent方法,来为AccessibilityService服务与AccessibilityEvent事件赋值。
  private AccessibilityService mAccessibilityService;
  private AccessibilityEvent mAccessibilityEvent;
  /**
   * 更新事件
   * @param service
   * @param event
   */
  public void updateEvent(AccessibilityService service, AccessibilityEvent event) {
    if (mAccessibilityService == null && service != null){
      mAccessibilityService = service;
    }
    if (event != null){
      mAccessibilityEvent = event;
    }
  }
  1. 对AccessibilityService与AccessibilityEvent赋值之后就可以正常使用了。clickText方法的完整内容代码:
  /**
   * 根据Text搜索所有符合条件的节点,模糊搜索方式
   * @param text
   * @return
   */
  public boolean clickText(String text) {
    AccessibilityNodeInfo nodeInfo = getRootNodeInfo();
    if (nodeInfo!=null){
      List<AccessibilityNodeInfo> nodeInfos =
          nodeInfo.findAccessibilityNodeInfosByText(text);
      return performClick(nodeInfos);
    }
    return false;
  }

/**
   * 获取根节点
   * @return
   */
  private AccessibilityNodeInfo getRootNodeInfo() {
    Log.e(TAG, "getRootNodeInfo: ");
    AccessibilityEvent curEvent = mAccessibilityEvent;
    AccessibilityNodeInfo nodeInfo = null;
    if (Build.VERSION.SDK_INT >= 16){
      if (mAccessibilityService!=null){
        // 获得窗体根节点
        nodeInfo = mAccessibilityService.getRootInActiveWindow();
      }
    }else {
      nodeInfo = curEvent.getSource();
    }
    return nodeInfo;
  }

  /**
   * 模拟点击
   * @param nodeInfos
   * @return true 成功; false 失败。
   */
  private boolean performClick(List<AccessibilityNodeInfo> nodeInfos) {
    if (nodeInfos!=null && !nodeInfos.isEmpty()){// 判断是否非空
      AccessibilityNodeInfo nodeInfo;
      for (int i=0;i<nodeInfos.size();i++){
        nodeInfo = nodeInfos.get(i);// 获得要点击的View
        // 进行模拟点击
        if (nodeInfo.isEnabled()){// 如果可以点击
          return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        }
      }
    }
    return false;
  }

getRootNodeInfo()返回的AccessibilityNodeInfo可以对它进行遍历,查询它的子节点

    AccessibilityNodeInfo nodeInfo = getRootNodeInfo();
    if (nodeInfo!=null){
      for (int i=0;i<nodeInfo.getChildCount();i++){
        AccessibilityNodeInfo child = nodeInfo.getChild(i);
        Log.e(TAG, "clickText: "+child.toString());
      }
    }
上一篇:细说一下RedisTemplate的使用方法(十)


下一篇:linux系统 网站木马后门Webshell查杀工具源码