autojs编写的toast例子,无需root权限就能运行,需要开启无障碍服务

说明

本文提供的代码仅供参考。不建议用于生产环境。
可能有些地方在最新版本的Auto.js上面需要做修改,才能运行。

Auto.js简介

Auto.js是利用安卓系统的“辅助功能”实现类似于按键精灵一样,可以通过代码模拟一系列界面动作的辅助工作。
与“按键精灵”不同的是,它的模拟动作并不是简单的使用在界面定坐标点来实现,而是类似与win一般,找窗口句柄来实现的。

Auto.js使用JavaScript作为脚本语言,目前使用Rhino 1.7.7.2作为脚本引擎,支持ES5与部分ES6特性。

推荐教程

Auto.js Pro安卓全分辨率免ROOT引流脚本开发视频教程(HD超清1080p)

开发文档

Auto.js Pro开发文档
文档尚在完善中,可能有文档描述和代码实际行为有出入的情况。

为什么要使用Auto.js Pro开发脚本,有什么特点?

吸引我使用Auto.js Pro的原因有很多。最主要的几个原因是:

  • Auto.js Pro能开发免ROOT的安卓脚本
  • Auto.js Pro基于节点操作,能开发全分辨率的脚本,自动适配各种安卓机型
  • Auto.js Pro丰富的UI组件,能自定义各种样式的安卓界面
  • Auto.js Pro使用的javascript的语法比较优雅,代码可读性强
  • Auto.js Pro的命令库非常的丰富,接口比较多
  • Auto.js Pro脚本文件体积比较小。1000行的代码,打包后的apk文件只有3-5M,还没有广告

示例代码

//此代码由飞云脚本圈整理提供(www.feiyunjs.com)

//android.widget.Toast.makeText(context, "asdf", 1).show();


var Toast = new android.widget.Toast(context);
Toast.setDuration(1);
var textview = new android.widget.TextView(context);
textview.setGravity(android.view.Gravity.CENTER);
threads.start(function() {
    try{
    textview.setText(String(new Date()));
    textview.setTextColor(colors.rgb(255, 0, 0));
    textview.setBackgroundColor(反色(colors.rgb(255, 0, 0)));
    Toast.setView(textview);
    Toast.show();
    sleep(10000);
    Toast.cancel();
    textview.setText(String(new Date()));
    textview.setTextColor(colors.rgb(0, 255, 0));
    textview.setBackgroundColor(反色(colors.rgb(0, 255, 0)));
    Toast.setView(textview);
    Toast.show();
    sleep(10000);
    Toast.cancel();
    textview.setText(String(new Date()));
    textview.setTextColor(colors.rgb(0, 0, 255));
    textview.setBackgroundColor(反色(colors.rgb(0, 0, 255)));
    Toast.setView(textview);
    Toast.show();
    sleep(10000);
    Toast.cancel();
    }catch(e){log(e);};
});



function 反色(color) {
    return (-1 - colors.argb(0, colors.red(color), colors.green(color), colors.blue(color)));
};

你可能感兴趣的:(脚本源码)