Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android輔助功能AccessibilityService與搶紅包輔助

Android輔助功能AccessibilityService與搶紅包輔助

編輯:關於Android編程

推薦閱讀:Android中微信搶紅包插件原理解析及開發思路

搶紅包的原理都差不多,一般是用Android的輔助功能(AccessibilityService類)先監聽通知欄事件或窗口變化事件來查找紅包關鍵字然後去模擬點擊或打開紅包。

下面附上源碼,程序已實現自動搶紅包,鎖屏黑屏狀態自動解鎖亮屏,Android4.X測試通過。函數具體功能請看詳細注釋。
注:在聊天界面收到紅包不會自動打開,因為通知欄沒有消息提示從而監聽不了,此時只需手動點一下即可。其他未知情況請自行用LogCat調試,源碼已經有相關的調試信息。軟件僅供學習娛樂。

<pre ><span ></span><pre >import java.util.Calendar; 
import java.util.List; 
import android.accessibilityservice.AccessibilityService; 
import android.annotation.SuppressLint; 
import android.app.KeyguardManager; 
import android.app.KeyguardManager.KeyguardLock; 
import android.app.Notification; 
import android.app.PendingIntent; 
import android.app.PendingIntent.CanceledException; 
import android.content.Context; 
import android.media.MediaPlayer; 
import android.os.PowerManager; 
import android.util.Log; 
import android.view.accessibility.AccessibilityEvent; 
import android.view.accessibility.AccessibilityNodeInfo; 
import android.widget.Toast; 
public class Demo extends AccessibilityService {
private boolean canGet = false;//能否點擊紅包
private boolean enableKeyguard = true;//默認有屏幕鎖
//窗口狀態
private static final int WINDOW_NONE = 0;
private static final int WINDOW_LUCKYMONEY_RECEIVEUI = 1;
private static final int WINDOW_LUCKYMONEY_DETAIL = 2;
private static final int WINDOW_LAUNCHER = 3;
private static final int WINDOW_OTHER = -1;
//當前窗口
private int mCurrentWindow = WINDOW_NONE;
//鎖屏、解鎖相關
private KeyguardManager km;
private KeyguardLock kl;
//喚醒屏幕相關
private PowerManager pm;
private PowerManager.WakeLock wl = null;
//播放提示聲音
private MediaPlayer player;
public void playSound(Context context) {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
//夜間不播放提示音
if(hour > 7 && hour < 22) { 
player.start();
}
}
//喚醒屏幕和解鎖
private void wakeAndUnlock(boolean unLock)
{
if(unLock)
{
//若為黑屏狀態則喚醒屏幕
if(!pm.isScreenOn()) {
//獲取電源管理器對象,ACQUIRE_CAUSES_WAKEUP這個參數能從黑屏喚醒屏幕
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "bright");
//點亮屏幕
wl.acquire();
Log.i("demo", "亮屏");
}
//若在鎖屏界面則解鎖直接跳過鎖屏
if(km.inKeyguardRestrictedInputMode()) {
//設置解鎖標志,以判斷搶完紅包能否鎖屏
enableKeyguard = false;
//解鎖
kl.disableKeyguard();
Log.i("demo", "解鎖");
}
}
else
{
//如果之前解過鎖則加鎖以恢復原樣
if(!enableKeyguard) {
//鎖屏
kl.reenableKeyguard();
Log.i("demo", "加鎖");
}
//若之前喚醒過屏幕則釋放之使屏幕不保持常亮
if(wl != null) {
wl.release();
wl = null;
Log.i("demo", "關燈");
}
}
}
//通過文本查找節點
public AccessibilityNodeInfo findNodeInfosByText(AccessibilityNodeInfo nodeInfo, String text) {
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(text);
if(list == null || list.isEmpty()) {
return null;
}
return list.get(0);
}
//模擬點擊事件
public void performClick(AccessibilityNodeInfo nodeInfo) {
if(nodeInfo == null) {
return;
}
if(nodeInfo.isClickable()) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
} else {
performClick(nodeInfo.getParent());
}
}
//模擬返回事件
public void performBack(AccessibilityService service) {
if(service == null) {
return;
}
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
}
//實現輔助功能
@Override 
public void onAccessibilityEvent(AccessibilityEvent event) { 
int eventType = event.getEventType();
Log.i("demo", Integer.toString(eventType));
switch (eventType) { 
//第一步:監聽通知欄消息 
case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: 
List<CharSequence> texts = event.getText(); 
if (!texts.isEmpty()) { 
for (CharSequence text : texts) { 
String content = text.toString(); 
Log.i("demo", "text:"+content);
//收到紅包提醒
if (content.contains("[微信紅包]")||content.contains("[QQ紅包]")) { 
//模擬打開通知欄消息 
if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
//播放提示音
playSound(this);
//若是微信紅包則解鎖並自動打開,若是qq紅包則只提示並跳轉到有紅包的聊天界面,暫未實現qq紅包自動領取功能
if(content.contains("[微信紅包]"))
wakeAndUnlock(true);
Log.i("demo", "canGet=true");
canGet = true;
try {
Notification notification = (Notification) event.getParcelableData(); 
PendingIntent pendingIntent = notification.contentIntent; 
pendingIntent.send();
} catch (CanceledException e) { 
e.printStackTrace(); 
}
}
break;
} 
} 
} 
break;
//第二步:監聽是否進入微信紅包消息界面 
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
String className = event.getClassName().toString();
if (className.equals("com.tencent.mm.ui.LauncherUI")) { 
mCurrentWindow = WINDOW_LAUNCHER;
//開始搶紅包 
Log.i("demo", "准備搶紅包...");
getPacket();
} else if (className.equals("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI")) {
mCurrentWindow = WINDOW_LUCKYMONEY_RECEIVEUI;
//開始打開紅包
Log.i("demo", "打開紅包");
openPacket();
wakeAndUnlock(false);
} else if(className.equals("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI")) {
mCurrentWindow = WINDOW_LUCKYMONEY_DETAIL;
//返回以方便下次收紅包
Log.i("demo", "返回");
performBack(this);
} else {
mCurrentWindow = WINDOW_OTHER;
}
break;
case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
if(mCurrentWindow != WINDOW_LAUNCHER) { //不在聊天界面或聊天列表,不處理
return;
}
if(canGet) {
getPacket();
}
break;
}
}
//找到紅包並點擊
@SuppressLint("NewApi") 
private void getPacket() { 
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); 
if (nodeInfo == null) { 
return; 
}
// 找到領取紅包的點擊事件 
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("領取紅包");
if(list != null ) {
if(list.isEmpty()) {
Log.i("demp", "領取列表為空");
// 從消息列表查找紅包
AccessibilityNodeInfo node = findNodeInfosByText(nodeInfo, "[微信紅包]");
if(node != null) {
canGet = true;
performClick(node);
}
}
else {
if(canGet) {
//最新的紅包領起
AccessibilityNodeInfo node = list.get(list.size() - 1);
performClick(node);
Log.i("demo", "canGet=false");
canGet = false;
}
}
}
}
//打開紅包
@SuppressLint("NewApi") 
private void openPacket() { 
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if(nodeInfo == null) {
return;
}
Log.i("demo", "查找打開按鈕...");
AccessibilityNodeInfo targetNode = null;
//如果紅包已經被搶完則直接返回
targetNode = findNodeInfosByText(nodeInfo, "看看大家的手氣");
if(targetNode != null) {
performBack(this);
return;
}
//通過組件名查找開紅包按鈕,還可通過組件id直接查找但需要知道id且id容易隨版本更新而變化,舊版微信還可直接搜“開”字找到按鈕
if(targetNode == null) { 
Log.i("demo", "打開按鈕中...");
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
AccessibilityNodeInfo node = nodeInfo.getChild(i);
if("android.widget.Button".equals(node.getClassName())) {
targetNode = node;
break;
}
}
}
//若查找到打開按鈕則模擬點擊
if(targetNode != null) {
final AccessibilityNodeInfo n = targetNode;
performClick(n);
} 
} 
@Override
public void onInterrupt() {
Toast.makeText(this, "搶紅包服務被中斷啦~", Toast.LENGTH_LONG).show();
}
@Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.i("demo", "開啟");
//獲取電源管理器對象
pm=(PowerManager)getSystemService(Context.POWER_SERVICE);
//得到鍵盤鎖管理器對象
km= (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
//初始化一個鍵盤鎖管理器對象
kl = km.newKeyguardLock("unLock");
//初始化音頻
player = MediaPlayer.create(this, R.raw.songtip_m);

Toast.makeText(this, "_已開啟搶紅包服務_", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("demo", "關閉");
wakeAndUnlock(false);
Toast.makeText(this, "_已關閉搶紅包服務_", Toast.LENGTH_LONG).show();
}
}

AndroidManifest.xml中聲明相關服務和權限

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> 
<application 
android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@style/AppTheme" > 
<pre name="code" class="html"><service 
android:name="com.example.test.Demo" android:enabled="true" android:exported="true" android:label="@string/app_name" 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="@layout/accessibility_config"/></service></application>

accessibility_config.xml服務配置內容如下

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" 
android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged|typeWindowContentChanged" 
android:accessibilityFeedbackType="feedbackGeneric" 
android:accessibilityFlags="flagDefault" 
android:canRetrieveWindowContent="true" 
android:description="@string/desc" 
android:notificationTimeout="100" 
android:packageNames= "com.tencent.mm,com.tencent.mobileqq" /> 

其中description為輔助功能的描述內容,packageNames為監聽的程序包名,此處只監聽微信和QQ的accessibilityEventTypes

以上所述是針對Android輔助功能AccessibilityService與搶紅包輔助的相關知識,希望對大家有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved