Android实现微信自动抢红包的程序
简单实现了微信自动抢红包的服务,原理就是根据关键字找到相应的View,然后自动点击。主要是用到AccessibilityService这个辅助服务,基本可以满足自动抢红包的功能,但是有些逻辑需要优化,比如,拆完一个红包后,必须手动点击返回键,才能进行下一次自动抢红包。
AndroidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="com.jackie.webchatenvelope"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:enabled="true" android:exported="true" android:label="@string/app_name" android:name=".EnvelopeService" android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"> <intent-filter> <actionandroid:name="android.accessibilityservice.AccessibilityService"/> </intent-filter> <meta-data android:name="android.accessibilityservice" android:resource="@xml/envelope_service_config"/> </service> </application> </manifest>
envelope_service_config.xml
<?xmlversion="1.0"encoding="utf-8"?> <accessibility-servicexmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged" android:accessibilityFeedbackType="feedbackGeneric" android:accessibilityFlags="" android:canRetrieveWindowContent="true" android:description="@string/accessibility_description" android:notificationTimeout="100" android:packageNames="com.tencent.mm"/>
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:layout_centerInParent="true" android:textSize="18sp" android:text="打开辅助服务"/> </RelativeLayout>
MainActivity.java
packagecom.jackie.webchatenvelope;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.Toast;
publicclassMainActivityextendsActivity{
privateButtonstartBtn;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn=(Button)findViewById(R.id.start);
startBtn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
try{
//打开系统设置中辅助功能
Intentintent=newIntent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
Toast.makeText(MainActivity.this,"找到抢红包,然后开启服务即可",Toast.LENGTH_LONG).show();
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
@Override
publicbooleanonCreateOptionsMenu(Menumenu){
//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
getMenuInflater().inflate(R.menu.menu_main,menu);
returntrue;
}
@Override
publicbooleanonOptionsItemSelected(MenuItemitem){
//Handleactionbaritemclickshere.Theactionbarwill
//automaticallyhandleclicksontheHome/Upbutton,solong
//asyouspecifyaparentactivityinAndroidManifest.xml.
intid=item.getItemId();
//noinspectionSimplifiableIfStatement
if(id==R.id.action_settings){
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
}
EnvelopeService.java
packagecom.jackie.webchatenvelope;
importandroid.accessibilityservice.AccessibilityService;
importandroid.annotation.TargetApi;
importandroid.app.Notification;
importandroid.app.PendingIntent;
importandroid.os.Build;
importandroid.os.Handler;
importandroid.util.Log;
importandroid.view.accessibility.AccessibilityEvent;
importandroid.view.accessibility.AccessibilityManager;
importandroid.view.accessibility.AccessibilityNodeInfo;
importandroid.widget.Toast;
importjava.util.List;
/**
*<p>CreatedbyAdministrator</p>
*<p/>
*抢红包外挂服务
*/
publicclassEnvelopeServiceextendsAccessibilityService{
staticfinalStringTAG="Jackie";
/**
*微信的包名
*/
staticfinalStringWECHAT_PACKAGENAME="com.tencent.mm";
/**
*红包消息的关键字
*/
staticfinalStringENVELOPE_TEXT_KEY="[微信红包]";
Handlerhandler=newHandler();
@Override
publicvoidonAccessibilityEvent(AccessibilityEventevent){
finalinteventType=event.getEventType();
Log.d(TAG,"事件---->"+event);
//通知栏事件
if(eventType==AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){
List<CharSequence>texts=event.getText();
if(!texts.isEmpty()){
for(CharSequencet:texts){
Stringtext=String.valueOf(t);
if(text.contains(ENVELOPE_TEXT_KEY)){
openNotification(event);
break;
}
}
}
}elseif(eventType==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
openEnvelope(event);
}
}
/*@Override
protectedbooleanonKeyEvent(KeyEventevent){
//returnsuper.onKeyEvent(event);
returntrue;
}*/
@Override
publicvoidonInterrupt(){
Toast.makeText(this,"中断抢红包服务",Toast.LENGTH_SHORT).show();
}
@Override
protectedvoidonServiceConnected(){
super.onServiceConnected();
Toast.makeText(this,"连接抢红包服务",Toast.LENGTH_SHORT).show();
}
privatevoidsendNotificationEvent(){
AccessibilityManagermanager=(AccessibilityManager)getSystemService(ACCESSIBILITY_SERVICE);
if(!manager.isEnabled()){
return;
}
AccessibilityEventevent=AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
event.setPackageName(WECHAT_PACKAGENAME);
event.setClassName(Notification.class.getName());
CharSequencetickerText=ENVELOPE_TEXT_KEY;
event.getText().add(tickerText);
manager.sendAccessibilityEvent(event);
}
/**
*打开通知栏消息
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
privatevoidopenNotification(AccessibilityEventevent){
if(event.getParcelableData()==null||!(event.getParcelableData()instanceofNotification)){
return;
}
//以下是精华,将微信的通知栏消息打开
Notificationnotification=(Notification)event.getParcelableData();
PendingIntentpendingIntent=notification.contentIntent;
try{
pendingIntent.send();
}catch(PendingIntent.CanceledExceptione){
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
privatevoidopenEnvelope(AccessibilityEventevent){
if("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassName())){
//点中了红包,下一步就是去拆红包
checkKey1();
}elseif("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassName())){
//拆完红包后看详细的纪录界面
//nonething
}elseif("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())){
//在聊天界面,去点中红包
checkKey2();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
privatevoidcheckKey1(){
AccessibilityNodeInfonodeInfo=getRootInActiveWindow();
if(nodeInfo==null){
Log.w(TAG,"rootWindow为空");
return;
}
List<AccessibilityNodeInfo>list=nodeInfo.findAccessibilityNodeInfosByText("拆红包");
for(AccessibilityNodeInfon:list){
n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
privatevoidcheckKey2(){
AccessibilityNodeInfonodeInfo=getRootInActiveWindow();
if(nodeInfo==null){
Log.w(TAG,"rootWindow为空");
return;
}
List<AccessibilityNodeInfo>list=nodeInfo.findAccessibilityNodeInfosByText("领取红包");
if(list.isEmpty()){
list=nodeInfo.findAccessibilityNodeInfosByText(ENVELOPE_TEXT_KEY);
for(AccessibilityNodeInfon:list){
Log.i(TAG,"-->微信红包:"+n);
n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
break;
}
}else{
//最新的红包领起
for(inti=list.size()-1;i>=0;i--){
AccessibilityNodeInfoparent=list.get(i).getParent();
Log.i(TAG,"-->领取红包:"+parent);
if(parent!=null){
parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
break;
}
}
}
}
}
以上所述是针对Android实现微信自动抢红包的程序,希望对大家有所帮助。