Android 輔助功能 -搶紅包 本篇文章主要介紹下通過android輔助功能的方式來實現類似搶紅包的功能. 1:許可權聲明 <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" tools:ign ...
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中聲明服務.
<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 發佈!