隐藏

Auto.js一些高级用法

发布:2023/11/21 11:39:23作者:管理员 来源:本站 浏览次数:306

1、判断本脚本是否重复运行


my_count = 0;

ne = engines.myEngine();

my_path = ne.mTags.get("execute_path") + "/" + ne.source;

engines.all().forEach(e => {

   path = e.mTags.get("execute_path") + "/" + e.source;

   if(path == my_path) my_count++;

});

if (my_count > 1) toast("本脚本重复运行");


 


   在坐标位置显示一个十字架,提示2秒后关闭,方便调试


function showXyTap(x,y){

   var xy = floaty.window(

       <frame gravity="center" w="30" h="30" alpha="0.6">

           <View w="2" h="30" bg="#ff00e4"></View>

           <View w="30" h="2" bg="#00ff42"></View>

       </frame>

   );

   xy.setPosition(Math.abs(x-15),Math.abs(y-15));

   setTimeout(()=>{

       xy.close();

   }, 2000);

}


 


   监控脚本是否卡在某界面不动,发现此情况重启脚本


function Observer() {

   function unique(arr) {

       let newArr = [arr[0]];

       for (let i = 1; i < arr.length; i++) {

           let flag = false;

           for (var j = 0; j < newArr.length; j++) {

               if (arr[i] == newArr[j]) {

                   flag = true;

                   break;

               }

           }

           if (!flag) {

               newArr.push(arr[i]);

           }

       }

       return newArr;

   }

   currentActis = new Array();

   for (let c = 0; c < 100; c++) {

       sleep(500);

       currentActis[c] = currentActivity();

   }

   ac = unique(currentActis);

   if (ac.length == 1) {

       return false

   }

   return true

}


// 》》》》》》》》》》》》》》》》》》》 START

work_thread = threads.start(function () {

   Main();

});



observer_thread = threads.start(function () {

   while (true) {

       sleep(5000);

       if (!Observer()) {

           work_thread.interrupt();

           work_thread = threads.start(function () {

               console.setPosition(device.width / 2, device.height / 1.5);

               console.show();

               console.warn("Main线程在5秒后重启!");

               sleep(5000);

               console.hide();

               Main();

           });

       }

   }

});


 


   兼容安卓7.0以上与6.0以下的点击,6.0需要root


function clickHelper2(x, y) {

   showXyTap(x, y);

   if (IS_GT_SDK23) {

       click(x, y);

   } else {

       try {

           // 容易出毛病,toomanyevenliserException

           Tap(x, y);

           runtime.sleep(1000);

       } catch (e) {

           try {

               if (!ra) {

                   ra = new RootAutomator();

               }

               // 保险点击

               ra.tap(x, y, 1);

               runtime.sleep(800);

               // ra.press(x, y, 10, 1);

           } catch (e) {

               ErrorHandle("在此设备上需要Root权限才能运行本脚本!", true);

           }


       }

   }

}


 


   跳转到加QQ


               app.startActivity({

                   action: "android.intent.action.VIEW",

                   data: "mqqapi://car/show_pslcard?&uin=542999277"

               })


 


   提醒开启无障碍服务


ui.autoService.on("check", function (checked) {

   // 用户勾选无障碍服务的选项时,跳转到页面让用户去开启

   if (checked && auto.service == null) {

       app.startActivity({

           action: "android.settings.ACCESSIBILITY_SETTINGS"

       });

   }

   if (!checked && auto.service != null) {

       auto.service.disableSelf();

   }

});



  


   跳转到开启悬浮窗(在其它应用窗口上显示,貌似要先设置一下无障碍,才能成功跳这个)


       int = app.startActivity({

           packageName: "com.android.settings",

           className: "com.android.settings.Settings$AppDrawOverlaySettingsActivity",

           data: "package:" + auto.service.getPackageName().toString()

       });


 


8.结束autojs自己


var nowPid = android.os.Process.myPid();

var am = context.getSystemService(java.lang.Class.forName("android.app.ActivityManager"));

var list = am.getRunningAppProcesses();

for(var i=0;i<list.size();i++){

var info = list.get(i)

if(info.pid != nowPid){

kill(info.pid);

}

}

kill(nowPid);

function kill(pid){

  android.os.Process.killProcess(pid);

}


 


还有什么实用的功能可以说一下 后边补充