Android 辅助功能 -抢红包

Android 辅助功能 -抢红包

本篇文章主要介绍下通过android辅助功能的方式来实现类似抢红包的功能.

1:权限声明

<uses-permission
 android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"
 tools:ignore="ProtectedPermissions" />

2: 配置信息

xml目录下创建如下文件:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
 android:accessibilityEventTypes="typeAllMask"
 android:accessibilityFeedbackType="feedbackAllMask"
 android:accessibilityFlags="flagDefault"
 android:canRetrieveWindowContent="true"
 android:canRequestFilterKeyEvents="true"
 android:description="@string/desc"
 android:packageNames= "com.test.redpacket">
</accessibility-service>
  1. android:accessibilityEventTypes 定义服务要监听的事件类型
  2. android:accessibilityFeedbackType 定义了服务的反馈类型
  3. android:accessibilityFlags 定义服务的行为标志
  4. android:description 描述信息 (需要使用资源文件配置)
  5. android:canRetrieveWindowContent 定义了服务是否可以获取窗口内容
  6. android:packageNames定义了监听的包名

3: 自定义AccessibilityService

具体的代码如下:

public class RedPacketAccessibilityService extends AccessibilityService {
 private static final String TAG = "RedPacketAccessibilityService";
 @SuppressLint("LongLogTag")
 @Override
 public void onAccessibilityEvent(AccessibilityEvent event) {
 int action = event.getAction();
 Log.i(TAG, "onAccessibilityEvent: " + action);
 CharSequence packageName = event.getPackageName();
 Log.i(TAG, "onAccessibilityEvent: " + packageName);
 AccessibilityNodeInfo rootInActiveWindow = getRootInActiveWindow();
 if (rootInActiveWindow != null) {
 List<AccessibilityNodeInfo> nodeInfos = rootInActiveWindow.findAccessibilityNodeInfosByText("红包:恭喜发财");
 if (nodeInfos != null) {
 for (AccessibilityNodeInfo nodeInfo : nodeInfos) {
 nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
 }
 }
 }
 }
 @Override
 public void onInterrupt() {
 }
}

此处通过findAccessibilityNodeInfosByText来查找节点了, 我们还可以使用 findAccessibilityNodeInfosByViewId来实现.

4: 服务声明

在manifest.xml中声明服务.

<service
 android:name=".RedPacketAccessibilityService"
 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/acc" />
</service>

5: 测试代码

public class MainActivity extends AppCompatActivity {
 private Button btnSend;
 private TextView tvMsg;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 btnSend=findViewById(R.id.btn_send);
 tvMsg=findViewById(R.id.tv_msg);
 Intent serviceIntent = new Intent(this, RedPacketAccessibilityService.class);
 startService(serviceIntent);
 btnSend.setOnClickListener(v -> {
 tvMsg.setText("红包:恭喜发财");
 });
 tvMsg.setOnClickListener(v -> {
 tvMsg.setText("消息展示");
 Toast.makeText(MainActivity.this,"抢红包",Toast.LENGTH_SHORT).show();
 });
 }
}

本文由博客一文多发平台 OpenWrite 发布!

作者:夏沫琅琊原文地址:https://www.cnblogs.com/zhjing/p/18075500

%s 个评论

要回复文章请先登录注册