去除重复,多余判断,调整条件语句顺序,去除日志打印,进一步提高抢红包速度,秒杀其他抢红包软件,红包抢到没朋友
关键代码
package com.example.hook;
import java.util.List;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Vibrator;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;
@SuppressLint("NewApi")
public class HookService extends AccessibilityService {
/**
* 当通知栏改变或界面改变时会触发该方法
*/
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event == null)
return;
if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
List texts = event.getText();
for (CharSequence t : texts) {
if (t.toString().contains("[微信红包]")) {// 获取通知栏字符,若包含 [微信红包]
// 则模拟手指点击事件
handleNotificationChange(event);
break;
}
}
}
/**
* 下面内容可以借助 Dump View Hierarchy For UI Automator 去分析微信UI结构
*/
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
// System.out.println("TYPE_WINDOW_STATE_CHANGED --> "+event.getClassName());
if ("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())) {
// 在聊天界面,去点中红包
checkKey2();
} else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI"
.equals(event.getClassName())) {
// 点中了红包,下一步就是去拆红包
checkKey1();
} else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI"
.equals(event.getClassName())) {
// 拆完红包后看详细的纪录界面
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// ((Vibrator)getSystemService(Context.VIBRATOR_SERVICE)).vibrate(500);
}
}
}
private void checkKey1() {
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) {
return;
}
List 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) {
return;
}
List list = nodeInfo
.findAccessibilityNodeInfosByText("领取红包"); // 找到聊天界面中包含 领取红包
// 字符的控件
if (list.isEmpty()) {
list = nodeInfo.findAccessibilityNodeInfosByText("[微信红包]");
for (AccessibilityNodeInfo n : list) {
n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
break;
}
} else {
// 最新的红包领起
for (int i = list.size() - 1; i >= 0; i--) {
AccessibilityNodeInfo parent = list.get(i).getParent();
try {
// 调用performAction(AccessibilityNodeInfo.ACTION_CLICK)触发点击事件
parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void handleNotificationChange(AccessibilityEvent event) {
try {
Notification notification = (Notification) event
.getParcelableData();
PendingIntent pendingIntent = notification.contentIntent;
pendingIntent.send(); // 点击通知栏消息
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onInterrupt() {
// TODO Auto-generated method stub
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, START_STICKY, startId);
}
}
主Activity
package com.example.hook;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button qidong;
TextView info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), HookService.class));
qidong=(Button) findViewById(R.id.qidong);
info=(TextView) findViewById(R.id.info);
qidong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "请开启 疯狂抢红包BY Young", 1).show();
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (isAccessibilitySettingsOn(getApplicationContext())) {
qidong.setText("已启动 疯狂抢红包BY Young");
info.setText("已开启自动抢红包模式,您可以后台运行该APP,同时开启 微信接收新消息通知,并后台运行微信,同时屏幕保持常亮");
}else{
info.setText("");
qidong.setText("点击开启 疯狂抢红包BY Young");
}
}
// To check if service is enabled
public boolean isAccessibilitySettingsOn(Context context) {
int accessibilityEnabled = 0;
final String service = context.getPackageName() + "/" + HookService.class.getName();
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(context.getApplicationContext().getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (SettingNotFoundException e) {
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
if (accessabilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
} else {
// Log.v(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
}
布局
清单文件
配置文件 res下xml文件中
完整工程 http://pan.baidu.com/s/1gdnQXur