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>
- android:accessibilityEventTypes 定义服务要监听的事件类型
- android:accessibilityFeedbackType 定义了服务的反馈类型
- android:accessibilityFlags 定义服务的行为标志
- android:description 描述信息 (需要使用资源文件配置)
- android:canRetrieveWindowContent 定义了服务是否可以获取窗口内容
- 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中声明服务.文章来源:https://www.toymoban.com/news/detail-840290.html
<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.toymoban.com/news/detail-840290.html
到了这里,关于Android 辅助功能 -抢红包的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!