AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
wechatlog_service_config.xml
<?xml version="1.0" encoding="utf-8"?>
WeChatLogService.java
package com.mycompany.myapp;import android.accessibilityservice.AccessibilityService;import android.content.Intent;import android.graphics.Rect;import android.os.Environment;import android.text.TextUtils;import android.util.Log;import android.view.accessibility.AccessibilityEvent;import android.view.accessibility.AccessibilityNodeInfo;import android.widget.Toast;import java.util.List;import android.accessibilityservice.*;import android.graphics.*;import androidx.annotation.*;import android.os.*;import android.app.*;import android.content.*;import android.content.pm.*;public class WeChatLogService extends AccessibilityService{private int AA; public static WeChatLogService mService; @Override public void onAccessibilityEvent(AccessibilityEvent event){ int eventType = event.getEventType();AccessibilityNodeInfo rootNodeInfo = getRootInActiveWindow();List nodes1=rootNodeInfo.findAccessibilityNodeInfosByViewId("com.mycompany.myapp:id/main_button1");//Log.e("控件信息", AA + " " + nodes1.toString());if (nodes1.size() != 0){nodes1.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);}} @Override public void onInterrupt(){ Toast.makeText(this, "我快被终结了啊-----", Toast.LENGTH_SHORT).show(); } @Override protected void onServiceConnected(){ Toast.makeText(this, "服务已开启", Toast.LENGTH_SHORT).show(); super.onServiceConnected();weixin(); } @Override public boolean onUnbind(Intent intent){ Toast.makeText(this, "服务已被关闭", Toast.LENGTH_SHORT).show(); return super.onUnbind(intent); }public static boolean isStart() { return mService != null; }private void weixin(){PackageManager packageManager = getPackageManager();Intent intent = new Intent(); //跳转到下一页5 APP界面intent = packageManager.getLaunchIntentForPackage("com.tencent.mm");startActivity(intent);}}
activity_main.xml
MainActivity.java
package com.mycompany.myapp;import android.app.*;import android.os.*;import android.view.*;import android.view.View.*;import android.widget.*;import android.content.*;import android.provider.*;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过id标识符获取控件 Button button1=(Button)findViewById(R.id.main_button1); //创建点击事件 button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//Toast.makeText(MainActivity.this,"点击了按钮",Toast.LENGTH_SHORT).show();if (!WeChatLogService.isStart()) {try {startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));} catch (Exception e) {startActivity(new Intent(Settings.ACTION_SETTINGS));e.printStackTrace();}}}}); }}