实现微信自动向附近的人打招呼,实现收到指定账户推送文章时自动进入微信打开链接
生活随笔
收集整理的這篇文章主要介紹了
实现微信自动向附近的人打招呼,实现收到指定账户推送文章时自动进入微信打开链接
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
學(xué)習(xí)功能強(qiáng)大的AccessibilityService!!!
以下是本人根據(jù)自動(dòng)搶紅包的實(shí)現(xiàn)思路敲的用于微信自動(dòng)向附近的人打招呼的核心代碼
public class AutoService extends AccessibilityService implements View.OnClickListener {private static final String TAG = "test";
/**
* 微信的包名
*/
static final String WECHAT_PACKAGENAME = "com.tencent.mm";
/**
* 推送消息在通知欄的關(guān)鍵字,設(shè)置為推送賬號(hào)名,如【十點(diǎn)讀書】
*/
static final String PUSH_TEXT_KEY = "十點(diǎn)讀書";
/**
* 推送鏈接的關(guān)鍵字,所有推送鏈接的標(biāo)題都需要包含此關(guān)鍵字:如【深度好文】
*/
private static final String URL_TEXT_KEY = "深度好文";
/**
* 向附近的人自動(dòng)打招呼的內(nèi)容
*/
private String hello = "測(cè)試APP自動(dòng)打招呼功能,這是一條測(cè)試信息";
boolean startFunc2 = false;//標(biāo)記是否開啟自動(dòng)添加附近的人為好友的功能;
int i = 0;//記錄已打招呼的人數(shù)
int page=1;//記錄附近的人列表頁碼,初始頁碼為1
int prepos = -1;//記錄頁面跳轉(zhuǎn)來源,0--從附近的人頁面跳轉(zhuǎn)到詳細(xì)資料頁,1--從打招呼頁面跳轉(zhuǎn)到詳細(xì)資料頁
@Override
public void onAccessibilityEvent(final AccessibilityEvent event) {int eventType = event.getEventType();
//通知欄事件
//自動(dòng)打開推送的鏈接
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(PUSH_TEXT_KEY)) {openNotification(event);
openDelay(1000, URL_TEXT_KEY);
}}}}//自動(dòng)加人
if (!startFunc2) {return;
}if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && event.getClassName().equals("com.tencent.mm.ui.LauncherUI")) {//記錄打招呼人數(shù)置零
i = 0;
//當(dāng)前在微信聊天頁就點(diǎn)開發(fā)現(xiàn)
openNext("發(fā)現(xiàn)");
//然后跳轉(zhuǎn)到附近的人
openDelay(1000, "附近的人");
} else if (event.getClassName().equals("com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {prepos = 0;
//當(dāng)前在附近的人界面就點(diǎn)選人打招呼
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("米以內(nèi)");
Log.d("name", "附近的人列表人數(shù): " + list.size());
if (i < (list.size()*page) ){list.get(i%list.size()).performAction(AccessibilityNodeInfo.ACTION_CLICK);
list.get(i%list.size()).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
} else if (i == list.size()*page) {//本頁已全部打招呼,所以下滑列表加載下一頁,每次下滑的距離是一屏
for (int i = 0; i < nodeInfo.getChild(0).getChildCount(); i++) {if (nodeInfo.getChild(0).getChild(i).getClassName().equals("android.widget.ListView")) {AccessibilityNodeInfo node_lsv = nodeInfo.getChild(0).getChild(i);
node_lsv.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
page++;
new Thread(new Runnable() {@Override
public void run() {try {Thread.sleep(1000);
} catch (InterruptedException mE) {mE.printStackTrace();
}AccessibilityNodeInfo nodeInfo_ = getRootInActiveWindow();
List<AccessibilityNodeInfo> list_ = nodeInfo_.findAccessibilityNodeInfosByText("米以內(nèi)");
Log.d("name", "列表人數(shù): "+list_.size());
//滑動(dòng)之后,上一頁的最后一個(gè)item為當(dāng)前的第一個(gè)item,所以從第二個(gè)開始打招呼
list_.get(1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
list_.get(1).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
}}).start();
}}}} else if (event.getClassName().equals("com.tencent.mm.plugin.profile.ui.ContactInfoUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {if (prepos == 1) {//從打招呼界面跳轉(zhuǎn)來的,則點(diǎn)擊返回到附近的人頁面
performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
i++;
} else if (prepos == 0) {//從附近的人跳轉(zhuǎn)來的,則點(diǎn)擊打招呼按鈕
AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) {Log.w(TAG, "rootWindow為空");
return;
}List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("打招呼");
if (list.size() > 0) {list.get(list.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
list.get(list.size() - 1).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
} else {//如果遇到已加為好友的則界面的“打招呼”變?yōu)椤鞍l(fā)消息",所以直接返回上一個(gè)界面并記錄打招呼人數(shù)+1
performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
i++;
}}} else if (event.getClassName().equals("com.tencent.mm.ui.contact.SayHiEditUI") && eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {//當(dāng)前在打招呼頁面
prepos = 1;
//輸入打招呼的內(nèi)容并發(fā)送
inputHello(hello);
openNext("發(fā)送");
}//自動(dòng)從桌面打開微信,利用微信多開助手可實(shí)現(xiàn)多個(gè)微信賬號(hào)之間的切換
// if(topActivity.equals("com.huawei.android.launcher.Launcher")){
// openNext(event,"微信");
// AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
// nodeInfo.getChildCount();
// for (int i=0;i<nodeInfo.getChildCount();i++){
// String name=nodeInfo.getChild(i).getViewIdResourceName();
// }
// }
}/**
* 打開通知欄消息
*/
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();
}}/**
* 點(diǎn)擊匹配的nodeInfo
* @param str text關(guān)鍵字
*/
private void openNext(String str) {AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
if (nodeInfo == null) {Log.w(TAG, "rootWindow為空");
return;
}List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(str);
Log.d("name", "匹配個(gè)數(shù): " + list.size());
if (list.size() > 0) {list.get(list.size() - 1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
list.get(list.size() - 1).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
// if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI".equals(event.getClassName())) {
// //點(diǎn)中了紅包,下一步就是去拆紅包
// checkKey1();
// } else if ("com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI".equals(event.getClassName())) {
// //拆完紅包后看詳細(xì)的紀(jì)錄界面
// } else if ("com.tencent.mm.ui.LauncherUI".equals(event.getClassName())) {
// //在聊天界面,去點(diǎn)中紅包
// checkKey2();
// }
}//延遲打開界面
private void openDelay(final int delaytime, final String text) {new Thread(new Runnable() {@Override
public void run() {try {Thread.sleep(delaytime);
} catch (InterruptedException mE) {mE.printStackTrace();
}openNext(text);
}}).start();
}//自動(dòng)輸入打招呼內(nèi)容
private void inputHello(String hello) {AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
//找到當(dāng)前獲取焦點(diǎn)的view
AccessibilityNodeInfo target = nodeInfo.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
if (target == null) {Log.d(TAG, "inputHello: null");
return;
}ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", hello);
clipboard.setPrimaryClip(clip);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {target.performAction(AccessibilityNodeInfo.ACTION_PASTE);
}}@Override
public void onInterrupt() {Toast.makeText(this, "服務(wù)已中斷", Toast.LENGTH_SHORT).show();
}@Override
protected void onServiceConnected() {super.onServiceConnected();
Toast.makeText(this, "連接服務(wù)", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate() {super.onCreate();
createFloatView();
}WindowManager wm;
Button floatbtn;
//創(chuàng)建懸浮按鈕
private void createFloatView() {WindowManager.LayoutParams pl = new WindowManager.LayoutParams();
wm = (WindowManager) getSystemService(getApplication().WINDOW_SERVICE);
pl.type = WindowManager.LayoutParams.TYPE_PHONE;
pl.format = PixelFormat.RGBA_8888;
pl.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
pl.gravity = Gravity.RIGHT | Gravity.BOTTOM;
pl.x = 0;
pl.y = 0;
pl.width = 200;
pl.height = 200;
LayoutInflater inflater = LayoutInflater.from(this);
floatbtn = (Button) inflater.inflate(R.layout.floatbtn, null);
wm.addView(floatbtn, pl);
floatbtn.setOnClickListener(this);
}@Override
public void onClick(View v) {if (startFunc2) {floatbtn.setText("啟用加人");
} else {floatbtn.setText("停止加人");
}startFunc2 = !startFunc2;
}
}
啟用這個(gè)服務(wù)的代碼: //打開系統(tǒng)設(shè)置中輔助功能 Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent);添加權(quán)限: <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> 如果發(fā)現(xiàn)代碼有問題,請(qǐng)留言,或者遇到哪里沒明白的也可以在留言里交流溝通,本人看到了一定回復(fù)demo地址:點(diǎn)擊打開鏈接
啟用這個(gè)服務(wù)的代碼: //打開系統(tǒng)設(shè)置中輔助功能 Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent);添加權(quán)限: <uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" /> 如果發(fā)現(xiàn)代碼有問題,請(qǐng)留言,或者遇到哪里沒明白的也可以在留言里交流溝通,本人看到了一定回復(fù)demo地址:點(diǎn)擊打開鏈接
總結(jié)
以上是生活随笔為你收集整理的实现微信自动向附近的人打招呼,实现收到指定账户推送文章时自动进入微信打开链接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业安全攻击面分析工具
- 下一篇: ae等高线_AE插件-地形海拔轮廓等高线