欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现微信自动抢红包的程序

程序员文章站 2024-02-23 15:01:46
 简单实现了微信自动抢红包的服务,原理就是根据关键字找到相应的view, 然后自动点击。主要是用到accessibilityservice这个辅助服务,基本可以满...

 简单实现了微信自动抢红包的服务,原理就是根据关键字找到相应的view, 然后自动点击。主要是用到accessibilityservice这个辅助服务,基本可以满足自动抢红包的功能,但是有些逻辑需要优化,比如,拆完一个红包后,必须手动点击返回键,才能进行下一次自动抢红包。

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns: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> 
<action android:name="android.intent.action.main" /> 
<category android: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> 
<action android: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

<?xml version="1.0" encoding="utf-8"?> 
<accessibility-service xmlns: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

<relativelayout xmlns: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

package com.jackie.webchatenvelope; 
import android.app.activity; 
import android.content.intent; 
import android.os.bundle; 
import android.view.menu; 
import android.view.menuitem; 
import android.view.view; 
import android.widget.button; 
import android.widget.toast; 
public class mainactivity extends activity { 
private button startbtn; 
@override 
protected void oncreate(bundle savedinstancestate) { 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.activity_main); 
startbtn = (button) findviewbyid(r.id.start); 
startbtn.setonclicklistener(new view.onclicklistener() { 
@override 
public void onclick(view v) { 
try { 
//打开系统设置中辅助功能 
intent intent = new intent(android.provider.settings.action_accessibility_settings); 
startactivity(intent); 
toast.maketext(mainactivity.this, "找到抢红包,然后开启服务即可", toast.length_long).show(); 
} catch (exception e) { 
e.printstacktrace(); 
} 
} 
}); 
} 
@override 
public boolean oncreateoptionsmenu(menu menu) { 
// inflate the menu; this adds items to the action bar if it is present. 
getmenuinflater().inflate(r.menu.menu_main, menu); 
return true; 
} 
@override 
public boolean onoptionsitemselected(menuitem item) { 
// handle action bar item clicks here. the action bar will 
// automatically handle clicks on the home/up button, so long 
// as you specify a parent activity in androidmanifest.xml. 
int id = item.getitemid(); 
//noinspection simplifiableifstatement 
if (id == r.id.action_settings) { 
return true; 
} 
return super.onoptionsitemselected(item); 
} 
} 

envelopeservice.java

package com.jackie.webchatenvelope; 
import android.accessibilityservice.accessibilityservice; 
import android.annotation.targetapi; 
import android.app.notification; 
import android.app.pendingintent; 
import android.os.build; 
import android.os.handler; 
import android.util.log; 
import android.view.accessibility.accessibilityevent; 
import android.view.accessibility.accessibilitymanager; 
import android.view.accessibility.accessibilitynodeinfo; 
import android.widget.toast; 
import java.util.list; 
/** 
* <p>created by administrator</p> 
* <p/> 
* 抢红包外挂服务 
*/ 
public class envelopeservice extends accessibilityservice { 
static final string tag = "jackie"; 
/** 
* 微信的包名 
*/ 
static final string wechat_packagename = "com.tencent.mm"; 
/** 
* 红包消息的关键字 
*/ 
static final string envelope_text_key = "[微信红包]"; 
handler handler = new handler(); 
@override 
public void onaccessibilityevent(accessibilityevent event) { 
final int eventtype = event.geteventtype(); 
log.d(tag, "事件---->" + event); 
//通知栏事件 
if (eventtype == accessibilityevent.type_notification_state_changed) { 
list<charsequence> texts = event.gettext(); 
if (!texts.isempty()) { 
for (charsequence t : texts) { 
string text = string.valueof(t); 
if (text.contains(envelope_text_key)) { 
opennotification(event); 
break; 
} 
} 
} 
} else if (eventtype == accessibilityevent.type_window_state_changed) { 
openenvelope(event); 
} 
} 
/*@override 
protected boolean onkeyevent(keyevent event) { 
//return super.onkeyevent(event); 
return true; 
}*/ 
@override 
public void oninterrupt() { 
toast.maketext(this, "中断抢红包服务", toast.length_short).show(); 
} 
@override 
protected void onserviceconnected() { 
super.onserviceconnected(); 
toast.maketext(this, "连接抢红包服务", toast.length_short).show(); 
} 
private void sendnotificationevent() { 
accessibilitymanager manager = (accessibilitymanager) getsystemservice(accessibility_service); 
if (!manager.isenabled()) { 
return; 
} 
accessibilityevent event = accessibilityevent.obtain(accessibilityevent.type_notification_state_changed); 
event.setpackagename(wechat_packagename); 
event.setclassname(notification.class.getname()); 
charsequence tickertext = envelope_text_key; 
event.gettext().add(tickertext); 
manager.sendaccessibilityevent(event); 
} 
/** 
* 打开通知栏消息 
*/ 
@targetapi(build.version_codes.jelly_bean) 
private void opennotification(accessibilityevent event) { 
if (event.getparcelabledata() == null || !(event.getparcelabledata() instanceof notification)) { 
return; 
} 
//以下是精华,将微信的通知栏消息打开 
notification notification = (notification) event.getparcelabledata(); 
pendingintent pendingintent = notification.contentintent; 
try { 
pendingintent.send(); 
} catch (pendingintent.canceledexception e) { 
e.printstacktrace(); 
} 
} 
@targetapi(build.version_codes.jelly_bean) 
private void openenvelope(accessibilityevent event) { 
if ("com.tencent.mm.plugin.luckymoney.ui.luckymoneyreceiveui".equals(event.getclassname())) { 
//点中了红包,下一步就是去拆红包 
checkkey1(); 
} else if ("com.tencent.mm.plugin.luckymoney.ui.luckymoneydetailui".equals(event.getclassname())) { 
//拆完红包后看详细的纪录界面 
//nonething 
} else if ("com.tencent.mm.ui.launcherui".equals(event.getclassname())) { 
//在聊天界面,去点中红包 
checkkey2(); 
} 
} 
@targetapi(build.version_codes.jelly_bean) 
private void checkkey1() { 
accessibilitynodeinfo nodeinfo = getrootinactivewindow(); 
if (nodeinfo == null) { 
log.w(tag, "rootwindow为空"); 
return; 
} 
list<accessibilitynodeinfo> list = nodeinfo.findaccessibilitynodeinfosbytext("拆红包"); 
for (accessibilitynodeinfo n : list) { 
n.performaction(accessibilitynodeinfo.action_click); 
} 
} 
@targetapi(build.version_codes.jelly_bean) 
private void checkkey2() { 
accessibilitynodeinfo nodeinfo = getrootinactivewindow(); 
if (nodeinfo == null) { 
log.w(tag, "rootwindow为空"); 
return; 
} 
list<accessibilitynodeinfo> list = nodeinfo.findaccessibilitynodeinfosbytext("领取红包"); 
if (list.isempty()) { 
list = nodeinfo.findaccessibilitynodeinfosbytext(envelope_text_key); 
for (accessibilitynodeinfo n : list) { 
log.i(tag, "-->微信红包:" + n); 
n.performaction(accessibilitynodeinfo.action_click); 
break; 
} 
} else { 
//最新的红包领起 
for (int i = list.size() - 1; i >= 0; i--) { 
accessibilitynodeinfo parent = list.get(i).getparent(); 
log.i(tag, "-->领取红包:" + parent); 
if (parent != null) { 
parent.performaction(accessibilitynodeinfo.action_click); 
break; 
} 
} 
} 
} 
} 

以上所述是针对android实现微信自动抢红包的程序,希望对大家有所帮助。