auto.js蚂蚁森林收能量
-
在网上看了一些自动收能量的脚本
-
写了一个脚本
-
使用AutoJs运行
- AutoJS下载地址 :here
-
运行步骤如下:
- 1.解锁(点亮屏幕,切换到输入密码界面,输入密码)
- 2.打开支付宝
- 3.找到蚂蚁森林
- 4.收取自己的能量
- 5.偷取他人能量
-
参考资料:
-
源码:
var morningTime = "07:22";//自己运动能量生成时间
var startTime = "07:00";
var endTime = "7:35";
var screen_width = 1080; //设置屏幕的宽度,像素值
var screen_height = 2340; //设置屏幕的高度,像素值
sleep(2000);
unlock();
sleep(2000);
threads.start(function(){
//在子线程中调用observeKey()从而使按键事件处理在子线程执行
events.observeKey();
events.on("key_down", function(keyCode, events){
//音量键关闭脚本
if(keyCode == keys.volume_down){
toast("您选择退出脚本!")
sleep(2000);
exit();
}
});
});
mainEntrence();
//程序主入口
function mainEntrence(){
do{
//尝试打开支付宝
if (!openAlipay()) {
toastLog("打开支付宝失败,退出程序");
exit();
}
// 尝试进入自己的蚂蚁森林,若失败跳过
if(!enterMyMainPage()){
toastLog("尝试进入自己的蚂蚁森林,若失败退出程序");
exit();
}
if(!collectEnergy("收取自己能量中")){
toastLog("如果不在蚂蚁森林,退出程序");
exit();
}
// 找能量
while(findOthers()){
back();
sleep(300);
}
// 执行返回 4 次
whenComplete(4);
// 睡眠 1 秒,等待下一次收集
sleep(1000);
}while(checkTime());
exit();
}
/*
* 收其他人能量
* return 是否有能量可收
*/
function findOthers(){
if(textEndsWith("种树").exists()){
// 到了自己能量时间
if(myEnergyTime()){
collectEnergy("收取自己能量");
}
// 点击按钮寻找能量,不同手机需要更改位置参数
click(960,1570);
sleep(500);
return collectEnergy("收其他人能量中");
}
toastLog("没有能量了");
return false;
}
/**
* 解锁函数
*/
function unlock(){
if(!device.isScreenOn()){
//点亮屏幕
device.wakeUp();
//由于MIUI的解锁有变速检测,因此要点开时间以进入密码界面
sleep(1000);
swipe(500, 0, 500, 1900, 2000);
click(100,150);
//输入屏幕解锁密码,其他密码请自行修改
sleep(2000);
click(*,*);
sleep(500);
click(*,*);
sleep(500);
click(*,*);
sleep(500);
click(*,*);
sleep(500);
}
}
/**
* 收能量函数
* return 是否收取成功
*/
function collectEnergy(info) {
// 判断是否在蚂蚁森林
if(!textContains("最新动态").exists()){
return false;
}
for(var row=screen_height*0.256;row<screen_height*0.376;row+=80){
for(var col=screen_width*0.185;col<screen_width*0.815;col+=80){
sleep(100);
click(col,row);
}
}
toastLog(info);
sleep(100);
return true;
}
/**
* 从支付宝主页进入蚂蚁森林我的主页
* return 是否进入
*/
function enterMyMainPage(){
//五次尝试蚂蚁森林入
var i=0;
// 拉至顶端
swipe(screen_width*0.5,screen_height*0.5,screen_width*0.5,screen_height*0.25,500);
sleep(500);
swipe(screen_width*0.5,screen_height*0.25,screen_width*0.5,screen_height*0.5,500);
while (!textEndsWith("蚂蚁森林").exists() && !descEndsWith("蚂蚁森林").exists() && i<=5){
sleep(1000);
i++;
}
if(i>=5){
toastLog("没有找到蚂蚁森林入口,尝试中");
clickByTextDesc("全部",0);
sleep(2000);
swipe(screen_width*0.5,screen_height*0.3,screen_width*0.5,screen_height*0.7,1000);
sleep(2000);
swipe(screen_width*0.5,screen_height*0.3,screen_width*0.5,screen_height*0.7,1000);
sleep(2000);
}
clickByTextDesc("蚂蚁森林",0);
//等待进入自己的主页,10次尝试
sleep(3000);
i=0;
while (!textEndsWith("种树").exists() && !descEndsWith("种树").exists() && i<=10){
sleep(1000);
i++;
}
toastLog("第"+i+"次尝试进入自己主页");
if(i>=10){
toastLog("进入自己能量主页失败");
return false;
}
return true;
}
/**
* 自定义的点击函数
* @param {*} energyType
* @param {*} paddingY
*/
function clickByTextDesc(energyType,paddingY){
var clicked = false;
if(descEndsWith(energyType).exists()){
descEndsWith(energyType).find().forEach(function(pos){
var posb=pos.bounds();
if(posb.centerX()<0 || posb.centerY()-paddingY<0){
return false;
}
//toastLog(pos.id());
var str = pos.id();
if(str != null){
if(str.search("search") == -1){
click(posb.centerX(),posb.centerY()-paddingY);
//toastLog("get it 1");
clicked = true;
}
}else{
click(posb.centerX(),posb.centerY()-paddingY);
//toastLog("get it 2");
clicked = true;
}
sleep(100);
});
}
if(textEndsWith(energyType).exists() && clicked == false){
textEndsWith(energyType).find().forEach(function(pos){
var posb=pos.bounds();
if(posb.centerX()<0 || posb.centerY()-paddingY<0){
return false;
}
//toastLog(pos.id());
var str = pos.id();
if(str != null){
if(str.search("search") == -1){
click(posb.centerX(),posb.centerY()-paddingY);
//toastLog("get it 3");
clicked = true;
}
}else{
click(posb.centerX(),posb.centerY()-paddingY);
//toastLog("get it 4");
clicked = true;
}
sleep(100);
});
}
return clicked;
}
/**
* 结束后返回主页面
* @param value : 返回次数
*/
function whenComplete(value) {
toastLog("结束");
while(value>0){
back();
sleep(1500);
value--;
}
}
/**
* 检测当前时间是否在指定范围内
* return 是:否
*/
function checkTime(){
var now =new Date();
var hour=now.getHours();
var minu=now.getMinutes();
var time_a=startTime.split(":");
var time_b=endTime.split(":");
var timea = 60*Number(time_a[0])+Number(time_a[1]);
var timeb = 60*Number(time_b[0])+Number(time_b[1]);
var time = 60*hour + minu;
if(time>=timea && time<=timeb){
//sleep(2000);
return true;
}else{
return false;
}
}
/**
* 是否到了收自己能量的时间
*/
function myEnergyTime(){
var now =new Date();
var hour=now.getHours();
var minu=now.getMinutes();
var mytime=morningTime.split(":");
if(mytime[0]==hour && (mytime[1]==minu || mytime[1]==minu-1) ){
return true;
}else{
return false;
}
}
/**
* 唤醒支付宝
* return 是否成功
*/
function openAlipay(){
launchApp("支付宝");
toastLog("等待支付宝启动");
// sleep(3000);
var i=0;
while (!textEndsWith("扫一扫").exists() && !descEndsWith("扫一扫").exists() && i<=5){
sleep(2000);
// 点击首页,防止进入后在其他菜单
clickByTextDesc("首页",0);
i++;
}
toastLog("第"+i+"次尝试进入支付宝主页");
if(i>=5){
toastLog("没有找到支付宝首页");
sleep(1000);
return false;
}
return true;
}
问题:
- 1、定时任务失败:
- 开启后台弹出界面权限
- 查看auto.js 的应用行为记录
- 查找被拒绝的请求
- 将被拒绝项更改为始终允许