今天推荐是一个比较骚气的工具,自动发朋友圈(不用root手机),对于一天发10多条,甚至20条朋友圈的朋友,每次编辑比较麻烦。我之前也写过抢红包工具,当时上架应用市场时,又恰逢过年,那下载量,比我吃了一桶老坛酸菜面还爽。后来由于应用市场的限制,就下架了,后来反正也不是很么高超的技术,就开源在我的github上了,今天分享这个自动发朋友圈的工具,也是和抢红包原理类似。通过开启辅助工具。进行监控屏幕上的按钮。延伸想想,那些喜欢点赞的朋友,是不是可以稍微改下,就变成了自动打开朋友圈,挨个朋友点赞。想想有木有很激动。哈哈~话不多说,我们进入司徒文德分享的文章。原文链接:https://www.jianshu.com/p/8b4ea121d188
一、效果图
二、测试Android设备
我在这两款设备测试正常,其它设备如果有什么问题,欢迎给我反馈_
夜神模拟器(Android 4.3)、华为荣耀8青春版(Android 7.0)
三、原理
整个app的核心是AccessibilitySampleService类,它继承了AccessibilityService服务。在辅助功能打开之后,AccessibilitySampleService类会监听目标APP(微信)页面的状态变化。之后使用Android官方提供的UI Automator Viewer工具,查看目标APP(微信)的布局结构树。
根据rescoure-id搜索出相应的控件,再对这些控件执行点击、复制、粘贴等操作。
看下核心实现逻辑
@TargetApi(18)
public class AccessibilitySampleService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
flag = false;
}
private AccessibilityNodeInfo accessibilityNodeInfo;
/**
* 是否已经发送过朋友圈,true已经发送,false还未发送
*/
public static boolean flag = false;
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
SharedPreferences sharedPreferences = getSharedPreferences(Constant.WECHAT_STORAGE, Activity.MODE_PRIVATE);
int eventType = event.getEventType();
LogUtil.e(eventType + " " + Integer.toHexString(eventType) + " " + event.getClassName());
accessibilityNodeInfo = getRootInActiveWindow();
if (accessibilityNodeInfo == null) {
return;
}
switch (eventType) {
case AccessibilityEvent.TYPE_VIEW_SCROLLED:
if (!flag && event.getClassName().equals("android.widget.ListView")) {
clickCircleOfFriendsBtn();//点击发送朋友圈按钮
}
break;
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
if (event.getClassName().equals("com.tencent.mm.ui.LauncherUI")) {//第一次启动app
flag = false;
jumpToCircleOfFriends();//进入朋友圈页面
}
if (!flag && event.getClassName().equals("com.tencent.mm.plugin.sns.ui.SnsUploadUI")) {
String content = sharedPreferences.getString(Constant.CONTENT, "");
inputContentFinish(content);//写入要发送的朋友圈内容
}
break;
}
}
/**
* 跳进朋友圈
*/
private void jumpToCircleOfFriends() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<AccessibilityNodeInfo> accessibilityNodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("android:id/list");
if (accessibilityNodeInfoList != null && accessibilityNodeInfoList.size() != 0) {
AccessibilityNodeInfo accessibilityNodeInfo = accessibilityNodeInfoList.get(0);
if (accessibilityNodeInfo != null) {
accessibilityNodeInfo = accessibilityNodeInfo.getChild(1);
if (accessibilityNodeInfo != null) {
accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
}
}
}, 1500);
}
/**
* 写入朋友圈内容
*
* @param contentStr
*/
private void inputContentFinish(final String contentStr) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<AccessibilityNodeInfo> accessibilityNodeInfoList1 = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/dba");
if (accessibilityNodeInfoList1.size() > 0) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("text", contentStr);
if (clipboard == null) {
return;
}
clipboard.setPrimaryClip(clip);
accessibilityNodeInfoList1.get(0).performAction(AccessibilityNodeInfo.ACTION_FOCUS);
accessibilityNodeInfoList1.get(0).performAction(AccessibilityNodeInfo.ACTION_PASTE);
LogUtil.e("写入内容");
}
List<AccessibilityNodeInfo> accessibilityNodeInfoList =
accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/h5");//点击发送
if (accessibilityNodeInfoList == null || accessibilityNodeInfoList.size() == 0) {
LogUtil.e("发送朋友圈失败");
return;
}
accessibilityNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
Toast.makeText(getApplicationContext(), "发送朋友圈成功", Toast.LENGTH_LONG).show();
flag = true;//标记为已发送
}
}, 1500);
}
/**
* 选择图片
*
* @param startPicIndex 从第startPicIndex张开始选
* @param picCount 总共选picCount张
*/
private void choosePicture(final int startPicIndex, final int picCount) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
List<AccessibilityNodeInfo> accessibilityNodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/cyh");
for (int i = 0; i < accessibilityNodeInfoList.size(); i++) {
for (int j = startPicIndex; j < startPicIndex + picCount; j++) {
AccessibilityNodeInfo childNodeInfo = accessibilityNodeInfoList.get(i).getChild(j);
if (childNodeInfo != null) {
List<AccessibilityNodeInfo> childNodeInfoList = childNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/bnl");
if (childNodeInfoList != null && childNodeInfoList.size() != 0) {
childNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
}
}
}
List<AccessibilityNodeInfo> finishList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/h5");//点击确定
if (finishList != null && finishList.size() != 0) {
finishList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
LogUtil.e("完成图片选择!");
}
}
}, 1500);
}
/**
* 点击发送朋友圈按钮
*/
private void clickCircleOfFriendsBtn() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (accessibilityNodeInfo == null) {
return;
}
List<AccessibilityNodeInfo> accessibilityNodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/g1");
if (accessibilityNodeInfoList != null && accessibilityNodeInfoList.size() != 0 &&
accessibilityNodeInfoList.get(0) != null && accessibilityNodeInfoList.get(0).getParent() != null) {
accessibilityNodeInfoList.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
}
openAlbum();
}
}, 1500);
}
/**
* 打开相册
*/
private void openAlbum() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (accessibilityNodeInfo == null) {
return;
}
List<AccessibilityNodeInfo> accessibilityNodeInfoList = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/axk");
if (accessibilityNodeInfoList != null && accessibilityNodeInfoList.size() != 0 && accessibilityNodeInfoList.get(0).getChildCount() != 0) {
accessibilityNodeInfoList.get(0).getChild(1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
LogUtil.e("打开相册!");
SharedPreferences sharedPreferences = getSharedPreferences(Constant.WECHAT_STORAGE, Activity.MODE_PRIVATE);
if (sharedPreferences != null) {
int index = sharedPreferences.getInt(Constant.INDEX, 0);
int count = sharedPreferences.getInt(Constant.COUNT, 0);
choosePicture(index, count);
}
}
}
}, 1500);
}
@Override
public void onInterrupt() {
}
/**
* Called by the system to notify a Service that it is no longer used and is being removed. The
* service should clean up any resources it holds (threads, registered
* receivers, etc) at this point. Upon return, there will be no more calls
* in to this Service object and it is effectively dead. Do not call this method directly.
*/
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.e("服务被杀死!");
}
}
四、注意事项
1、Android设备必须安装微信app
2、Android Sdk Version>18
特别提醒(不需要获取root权限)
五、开源地址
点击【阅读原文】