查看: 711|回复: 0

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

发表于 2016-7-30 08:00:02

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

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.jackie.webchatenvelope" >
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:theme="@style/AppTheme" >
  9. <activity
  10. android:name=".MainActivity"
  11. android:label="@string/app_name" >
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. <service
  18. android:enabled="true"
  19. android:exported="true"
  20. android:label="@string/app_name"
  21. android:name=".EnvelopeService"
  22. android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
  23. <intent-filter>
  24. <action android:name="android.accessibilityservice.AccessibilityService"/>
  25. </intent-filter>
  26. <meta-data
  27. android:name="android.accessibilityservice"
  28. android:resource="@xml/envelope_service_config"/>
  29. </service>
  30. </application>
  31. </manifest>
复制代码
envelope_service_config.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged"
  4. android:accessibilityFeedbackType="feedbackGeneric"
  5. android:accessibilityFlags=""
  6. android:canRetrieveWindowContent="true"
  7. android:description="@string/accessibility_description"
  8. android:notificationTimeout="100"
  9. android:packageNames="com.tencent.mm" />
复制代码
activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity">
  10. <Button
  11. android:id="@+id/start"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:padding="10dp"
  15. android:layout_centerInParent="true"
  16. android:textSize="18sp"
  17. android:text="打开辅助服务"/>
  18. </RelativeLayout>
复制代码

MainActivity.java

  1. package com.jackie.webchatenvelope;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10. public class MainActivity extends Activity {
  11. private Button startBtn;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. startBtn = (Button) findViewById(R.id.start);
  17. startBtn.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. try {
  21. //打开系统设置中辅助功能
  22. Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
  23. startActivity(intent);
  24. Toast.makeText(MainActivity.this, "找到抢红包,然后开启服务即可", Toast.LENGTH_LONG).show();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. });
  30. }
  31. @Override
  32. public boolean onCreateOptionsMenu(Menu menu) {
  33. // Inflate the menu; this adds items to the action bar if it is present.
  34. getMenuInflater().inflate(R.menu.menu_main, menu);
  35. return true;
  36. }
  37. @Override
  38. public boolean onOptionsItemSelected(MenuItem item) {
  39. // Handle action bar item clicks here. The action bar will
  40. // automatically handle clicks on the Home/Up button, so long
  41. // as you specify a parent activity in AndroidManifest.xml.
  42. int id = item.getItemId();
  43. //noinspection SimplifiableIfStatement
  44. if (id == R.id.action_settings) {
  45. return true;
  46. }
  47. return super.onOptionsItemSelected(item);
  48. }
  49. }
复制代码

EnvelopeService.java

  1. package com.jackie.webchatenvelope;
  2. import android.accessibilityservice.AccessibilityService;
  3. import android.annotation.TargetApi;
  4. import android.app.Notification;
  5. import android.app.PendingIntent;
  6. import android.os.Build;
  7. import android.os.Handler;
  8. import android.util.Log;
  9. import android.view.accessibility.AccessibilityEvent;
  10. import android.view.accessibility.AccessibilityManager;
  11. import android.view.accessibility.AccessibilityNodeInfo;
  12. import android.widget.Toast;
  13. import java.util.List;
  14. /**
  15. * <p>Created by Administrator</p>
  16. * <p/>
  17. * 抢红包外挂服务
  18. */
  19. public class EnvelopeService extends AccessibilityService {
  20. static final String TAG = "Jackie";
  21. /**
  22. * 微信的包名
  23. */
  24. static final String WECHAT_PACKAGENAME = "com.tencent.mm";
  25. /**
  26. * 红包消息的关键字
  27. */
  28. static final String ENVELOPE_TEXT_KEY = "[微信红包]";
  29. Handler handler = new Handler();
  30. @Override
  31. public void onAccessibilityEvent(AccessibilityEvent event) {
  32. final int eventType = event.getEventType();
  33. Log.d(TAG, "事件---->" + event);
  34. //通知栏事件
  35. if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
  36. List<CharSequence> texts = event.getText();
  37. if (!texts.isEmpty()) {
  38. for (CharSequence t : texts) {
  39. String text = String.valueOf(t);
  40. if (text.contains(ENVELOPE_TEXT_KEY)) {
  41. openNotification(event);
  42. break;
  43. }
  44. }
  45. }
  46. } else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
  47. openEnvelope(event);
  48. }
  49. }
  50. /*@Override
  51. protected boolean onKeyEvent(KeyEvent event) {
  52. //return super.onKeyEvent(event);
  53. return true;
  54. }*/
  55. @Override
  56. public void onInterrupt() {
  57. Toast.makeText(this, "中断抢红包服务", Toast.LENGTH_SHORT).show();
  58. }
  59. @Override
  60. protected void onServiceConnected() {
  61. super.onServiceConnected();
  62. Toast.makeText(this, "连接抢红包服务", Toast.LENGTH_SHORT).show();
  63. }
  64. private void sendNotificationEvent() {
  65. AccessibilityManager manager = (AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE);
  66. if (!manager.isEnabled()) {
  67. return;
  68. }
  69. AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
  70. event.setPackageName(WECHAT_PACKAGENAME);
  71. event.setClassName(Notification.class.getName());
  72. CharSequence tickerText = ENVELOPE_TEXT_KEY;
  73. event.getText().add(tickerText);
  74. manager.sendAccessibilityEvent(event);
  75. }
  76. /**
  77. * 打开通知栏消息
  78. */
  79. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  80. private void openNotification(AccessibilityEvent event) {
  81. if (event.getParcelableData() == null || !(event.getParcelableData() instanceof Notification)) {
  82. return;
  83. }
  84. //以下是精华,将微信的通知栏消息打开
  85. Notification notification = (Notification) event.getParcelableData();
  86. PendingIntent pendingIntent = notification.contentIntent;
  87. try {
  88. pendingIntent.send();
  89. } catch (PendingIntent.CanceledException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  94. private void openEnvelope(AccessibilityEvent event) {
  95. if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassName())) {
  96. //点中了红包,下一步就是去拆红包
  97. checkKey1();
  98. } else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassName())) {
  99. //拆完红包后看详细的纪录界面
  100. //nonething
  101. } else if ("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())) {
  102. //在聊天界面,去点中红包
  103. checkKey2();
  104. }
  105. }
  106. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  107. private void checkKey1() {
  108. AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
  109. if (nodeInfo == null) {
  110. Log.w(TAG, "rootWindow为空");
  111. return;
  112. }
  113. List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("拆红包");
  114. for (AccessibilityNodeInfo n : list) {
  115. n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
  116. }
  117. }
  118. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  119. private void checkKey2() {
  120. AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
  121. if (nodeInfo == null) {
  122. Log.w(TAG, "rootWindow为空");
  123. return;
  124. }
  125. List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("领取红包");
  126. if (list.isEmpty()) {
  127. list = nodeInfo.findAccessibilityNodeInfosByText(ENVELOPE_TEXT_KEY);
  128. for (AccessibilityNodeInfo n : list) {
  129. Log.i(TAG, "-->微信红包:" + n);
  130. n.performAction(AccessibilityNodeInfo.ACTION_CLICK);
  131. break;
  132. }
  133. } else {
  134. //最新的红包领起
  135. for (int i = list.size() - 1; i >= 0; i--) {
  136. AccessibilityNodeInfo parent = list.get(i).getParent();
  137. Log.i(TAG, "-->领取红包:" + parent);
  138. if (parent != null) {
  139. parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. }
复制代码






回复

使用道具 举报