Android中微信抢红包插件原理解析及开发思路

2019-12-10 19:08:02刘景俊

1、android:accessibilityEventTypes="typeAllMask"
看属性名也差不多可以明白,这个是用来设置响应事件的类型,typeAllMask当然就是响应所有类型的事件了。当然还有单击、长按、滑动等。

2、android:accessibilityFeedbackType="feedbackSpoken"

设置回馈给用户的方式,有语音播出和振动。可以配置一些TTS引擎,让它实现发音。

3、android:notificationTimeout="100"

响应时间的设置就不用多说了

4、android:packageNames="com.example.android.apis"

可以指定响应某个应用的事件,这里因为要响应所有应用的事件,所以不填,默认就是响应所有应用的事件。比如我们写一个微信抢红包的辅助程序,就可以在这里填写微信的包名,便可以监听微信产生的事件了。

注意:

1、我们这些配置信息除了在xml中定义,同样也可以在代码中定义,我们一般都是在onServiceConnected()方法里进行

@Override 
protected void onServiceConnected() { 
AccessibilityServiceInfo info = getServiceInfo(); 
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK; 
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN; 
info.notificationTimeout = 100; 
setServiceInfo(info); 
info.packageNames = new String[]{"xxx.xxx.xxx", "yyy.yyy.yyy","...."}; 
setServiceInfo(info); 
super.onServiceConnected(); 
}

2、这里我们一般都会在这里写上我们需要监听的应用的包名,但是有时候我们需要监听多个应用,那么这时候我们该怎么办呢?

这时候我们可以这么做:

第一种:我们在代码中注册多个应用的包名,从而可以监听多个应用

@Override 
protected void onServiceConnected() { 
AccessibilityServiceInfo info = getServiceInfo(); 
//这里可以设置多个包名,监听多个应用 
info.packageNames = new String[]{"xxx.xxx.xxx", "yyy.yyy.yyy","...."}; 
setServiceInfo(info); 
super.onServiceConnected(); 
}