This commit is contained in:
hyb1996
2017-12-28 21:23:35 +08:00
parent 3647cef81b
commit ee0c7f4916
16 changed files with 2085 additions and 745 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -72,15 +72,23 @@
<h2>目录</h2> <h2>目录</h2>
<ul> <ul>
<li><span class="stability_undefined"><a href="#app_app">App</a></span><ul> <li><span class="stability_undefined"><a href="#app_app">App</a></span><ul>
<li><span class="stability_undefined"><a href="#app_app_launchapp_appname">app.launchApp(appName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_launch_packagename">app.launch(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_launchpackage_packagename">app.launchPackage(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_getpackagename_appname">app.getPackageName(appName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_getappname_packagename">app.getAppName(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_openappsetting_packagename">app.openAppSetting(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_viewfile_path">app.viewFile(path)</a></span></li> <li><span class="stability_undefined"><a href="#app_app_viewfile_path">app.viewFile(path)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_editfile_path">app.editFile(path)</a></span></li> <li><span class="stability_undefined"><a href="#app_app_editfile_path">app.editFile(path)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_uninstall_packagename">app.uninstall(packageName)</a></span></li> <li><span class="stability_undefined"><a href="#app_app_uninstall_packagename">app.uninstall(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_openurl_url">app.openUrl(url)</a></span></li> <li><span class="stability_undefined"><a href="#app_app_openurl_url">app.openUrl(url)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_takephoto_path">app.takePhoto(path)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_sendemail_options">app.sendEmail(options)</a></span></li> <li><span class="stability_undefined"><a href="#app_app_sendemail_options">app.sendEmail(options)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_intent_intent">app.intent(intent)</a></span></li> </ul>
<li><span class="stability_undefined"><a href="#app_app_startactivity_intent">app.startActivity(intent)</a></span></li> </li>
<li><span class="stability_undefined"><a href="#app_app_sendbroadcast_intent">app.sendBroadcast(intent)</a></span></li> <li><span class="stability_undefined"><a href="#app_intent">进阶: 意图Intent</a></span><ul>
<li><span class="stability_undefined"><a href="#app_app_intent_options">app.intent(options)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_startactivity_options">app.startActivity(options)</a></span></li>
<li><span class="stability_undefined"><a href="#app_app_sendbroadcast_options">app.sendBroadcast(options)</a></span></li>
</ul> </ul>
</li> </li>
</ul> </ul>
@@ -89,77 +97,146 @@
<div id="apicontent"> <div id="apicontent">
<h1>App<span><a class="mark" href="#app_app" id="app_app">#</a></span></h1> <h1>App<span><a class="mark" href="#app_app" id="app_app">#</a></span></h1>
<p>app模块提供一系列函数用于与其他应用交互。例如打开文件、拍照、发送邮件等。</p> <p>app模块提供一系列函数用于使用其他应用、与其他应用交互。例如发送意图、打开文件、发送邮件等。</p>
<p>同时提供了方便的基础函数startActivity和sendBroadcast用他们可完成app模块没有内置的和其他应用的交互。</p> <p>同时提供了方便的进阶函数startActivity和sendBroadcast用他们可完成app模块没有内置的和其他应用的交互。</p>
<h2>app.launchApp(appName)<span><a class="mark" href="#app_app_launchapp_appname" id="app_app_launchapp_appname">#</a></span></h2>
<div class="signature"><ul>
<li><code>appName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用名称</li>
</ul>
</div><p>通过应用名称启动应用。如果该名称对应的应用不存在则返回false; 否则返回true。如果该名称对应多个应用则只启动其中某一个。</p>
<p>该函数也可以作为全局函数使用。</p>
<pre><code>launchApp(&quot;Auto.js&quot;);
</code></pre><h2>app.launch(packageName)<span><a class="mark" href="#app_app_launch_packagename" id="app_app_launch_packagename">#</a></span></h2>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>通过应用包名启动应用。如果该包名对应的应用不存在则返回false否则返回true。</p>
<p>该函数也可以作为全局函数使用。</p>
<pre><code>//启动微信
launch(&quot;com.tencent.mm&quot;);
</code></pre><h2>app.launchPackage(packageName)<span><a class="mark" href="#app_app_launchpackage_packagename" id="app_app_launchpackage_packagename">#</a></span></h2>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>相当于<code>app.launch(packageName)</code></p>
<h2>app.getPackageName(appName)<span><a class="mark" href="#app_app_getpackagename_appname" id="app_app_getpackagename_appname">#</a></span></h2>
<div class="signature"><ul>
<li><code>appName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用名称</li>
</ul>
</div><p>获取应用名称对应的已安装的应用的包名。如果该找不到该应用返回null如果该名称对应多个应用则只返回其中某一个的包名。</p>
<p>该函数也可以作为全局函数使用。</p>
<pre><code>var name = getPackageName(&quot;QQ&quot;); //返回&quot;com.tencent.mobileqq&quot;
</code></pre><h2>app.getAppName(packageName)<span><a class="mark" href="#app_app_getappname_packagename" id="app_app_getappname_packagename">#</a></span></h2>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>获取应用包名对应的已安装的应用的名称。如果该找不到该应用返回null。</p>
<p>该函数也可以作为全局函数使用。</p>
<pre><code>var name = getAppName(&quot;com.tencent.mobileqq&quot;); //返回&quot;QQ&quot;
</code></pre><h2>app.openAppSetting(packageName)<span><a class="mark" href="#app_app_openappsetting_packagename" id="app_app_openappsetting_packagename">#</a></span></h2>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>打开应用的详情页(设置页)。如果找不到该应用返回false; 否则返回true。</p>
<p>该函数也可以作为全局函数使用。</p>
<h2>app.viewFile(path)<span><a class="mark" href="#app_app_viewfile_path" id="app_app_viewfile_path">#</a></span></h2> <h2>app.viewFile(path)<span><a class="mark" href="#app_app_viewfile_path" id="app_app_viewfile_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>path <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径</li> <li>path <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径</li>
</ul> </ul>
</div><p>用其他应用查看文件。</p> </div><p>用其他应用查看文件。文件不存在的情况由查看文件的应用处理。</p>
<h2>app.editFile(path)<span><a class="mark" href="#app_app_editfile_path" id="app_app_editfile_path">#</a></span></h2> <p>如果找不出可以查看该文件的应用,则抛出<code>ActivityNotException</code></p>
<pre><code>//查看文本文件
app.viewFile(&quot;/sdcard/1.txt&quot;);
</code></pre><h2>app.editFile(path)<span><a class="mark" href="#app_app_editfile_path" id="app_app_editfile_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>path <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径</li> <li>path <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径</li>
</ul> </ul>
</div><p>用其他应用编辑文件。</p> </div><p>用其他应用编辑文件。文件不存在的情况由编辑文件的应用处理。</p>
<h2>app.uninstall(packageName)<span><a class="mark" href="#app_app_uninstall_packagename" id="app_app_uninstall_packagename">#</a></span></h2> <p>如果找不出可以编辑该文件的应用,则抛出<code>ActivityNotException</code></p>
<pre><code>//编辑文本文件
app.editFile(&quot;/sdcard/1.txt/);
</code></pre><h2>app.uninstall(packageName)<span><a class="mark" href="#app_app_uninstall_packagename" id="app_app_uninstall_packagename">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>packageName <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li> <li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul> </ul>
</div><p>卸载应用。</p> </div><p>卸载应用。执行后会会弹出卸载应用的提示框。如果该包名的应用未安装,由应用卸载程序处理,可能弹出&quot;未找到应用&quot;的提示。</p>
<h2>app.openUrl(url)<span><a class="mark" href="#app_app_openurl_url" id="app_app_openurl_url">#</a></span></h2> <pre><code>//卸载QQ
app.uninstall(&quot;com.tencent.mobileqq&quot;);
</code></pre><h2>app.openUrl(url)<span><a class="mark" href="#app_app_openurl_url" id="app_app_openurl_url">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>url <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 网站的Url</li> <li><code>url</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 网站的Url,如果不以&quot;<a href="http://&quot;或&quot;https://&quot;开头则默认是&quot;http://&quot;。">http://&quot;&quot;https://&quot;开头则默认是&quot;http://&quot;</a></li>
</ul> </ul>
</div><p>用浏览器打开网站url。</p> </div><p>用浏览器打开网站url。</p>
<h2>app.takePhoto(path)<span><a class="mark" href="#app_app_takephoto_path" id="app_app_takephoto_path">#</a></span></h2> <p>如果没有安装浏览器应用,则抛出<code>ActivityNotException</code></p>
<div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 照片保存路径</li>
</ul>
</div><p>调用相机应用拍照完成后保存到路径path。</p>
<h2>app.sendEmail(options)<span><a class="mark" href="#app_app_sendemail_options" id="app_app_sendemail_options">#</a></span></h2> <h2>app.sendEmail(options)<span><a class="mark" href="#app_app_sendemail_options" id="app_app_sendemail_options">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 发送邮件的参数。包括:<ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 发送邮件的参数。包括:<ul>
<li>email <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 收件人的邮件地址。如果有多个收件人,则用字符串数组表示</li> <li><code>email</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 收件人的邮件地址。如果有多个收件人,则用字符串数组表示</li>
<li>cc <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 抄送收件人的邮件地址。如果有多个抄送收件人,则用字符串数组表示</li> <li><code>cc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 抄送收件人的邮件地址。如果有多个抄送收件人,则用字符串数组表示</li>
<li>bcc <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 密送收件人的邮件地址。如果有多个密送收件人,则用字符串数组表示</li> <li><code>bcc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 密送收件人的邮件地址。如果有多个密送收件人,则用字符串数组表示</li>
<li>subject <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 邮件主题(标题)</li> <li><code>subject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 邮件主题(标题)</li>
<li>text <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 邮件正文</li> <li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 邮件正文</li>
<li>attachment <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 附件的路径。如果有多个附件,则用字符串数组表示</li> <li><code>attachment</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 附件的路径。</li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>根据选项options调用邮箱应用发送邮件。这些选项均是可选的。</p> </div><p>根据选项options调用邮箱应用发送邮件。这些选项均是可选的。</p>
<h2>app.intent(intent)<span><a class="mark" href="#app_app_intent_intent" id="app_app_intent_intent">#</a></span></h2> <p>如果没有安装邮箱应用,则抛出<code>ActivityNotException</code></p>
<pre><code>//发送邮件给10086@qq.com和10001@qq.com。
app.sendEmail({
email: [&quot;10086@qq.com&quot;, &quot;10001@qq.com&quot;],
subject: &quot;这是一个邮件标题&quot;,
text: &quot;这是邮件正文&quot;
});
</code></pre><h1>进阶: 意图Intent<span><a class="mark" href="#app_intent" id="app_intent">#</a></span></h1>
<p>Intent(意图) 是一个消息传递对象,您可以使用它从其他应用组件请求操作。尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个:</p>
<ul>
<li><p>启动活动(Activity)
Activity 表示应用中的一个&quot;屏幕&quot;。例如应用主入口都是一个Activity应用的功能通常也以Activity的形式独立例如微信的主界面、朋友圈、聊天窗口都是不同的Activity。通过将 Intent 传递给 startActivity(),您可以启动新的 Activity 实例。Intent 描述了要启动的 Activity并携带了任何必要的数据。</p>
</li>
<li><p>启动服务(Service)
Service 是一个不使用用户界面而在后台执行操作的组件。通过将 Intent 传递给 startService()您可以启动服务执行一次性操作例如下载文件。Intent 描述了要启动的服务,并携带了任何必要的数据。</p>
</li>
<li><p>传递广播:
广播是任何应用均可接收的消息。系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播。通过将 Intent 传递给 sendBroadcast()、sendOrderedBroadcast() 或 sendStickyBroadcast(),您可以将广播传递给其他应用。</p>
</li>
</ul>
<p>本模块提供了构建Intent的函数(<code>app.intent()</code>), 启动Activity的函数<code>app.startActivity()</code>, 发送广播的函数<code>app.sendBroadcast()</code></p>
<p>使用这些方法可以用来方便的调用其他应用。例如直接打开某个QQ号的个人卡片页打开某个QQ号的聊天窗口等。</p>
<pre><code>
</code></pre><h2>app.intent(options)<span><a class="mark" href="#app_app_intent_options" id="app_app_intent_options">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>intent <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 一个表示Intent对象其属性可以包括:<ul> <li>options <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 选项,包括:<ul>
<li>action <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 这个Intent的Action,比如&quot;android.intent.action.SEND&quot;</li> <li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 意图的Action指意图要完成的动作是一个字符串常量比如&quot;android.intent.action.SEND&quot;。当action&quot;android.intent.action&quot;开头时,可以省略前缀,直接用&quot;SEND&quot;代替。常见的action参见<code>常用的意图动作</code></li>
<li>type <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 这个Intent的MimeType比如&quot;text/plain&quot;</li> <li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 意图的MimeType表示和该意图直接相关的数据的类型,表示比如&quot;text/plain&quot;为纯文本类型。</li>
<li>data <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 这个Intent的Data(Uri)可以是文件路径或者Url等。</li> <li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 意图的Data表示和该意图直接相关的数据是一个Uri, 可以是文件路径或者Url等。例如要打开一个文件, action为&quot;android.intent.action.VIEW&quot;, data为&quot;file:///sdcard/1.txt&quot;</li>
<li>category \<Array\> 这个Intent的Category的字符串数组</li> <li><code>category</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 意图的类别。比较少用</li>
<li>packageName <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 目标包名</li> <li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 目标包名</li>
<li>className <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 目标Activity或Service等组件的名称</li> <li><code>className</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 目标Activity或Service等组件的名称</li>
<li>extras <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 以键值对构成的这个Intent的Extras。</li> <li><code>extras</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 以键值对构成的这个Intent的Extras(额外信息)。提供该意图的其他信息,例如发送邮件时的邮件标题、邮件正文</li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>返回用intent对象构造的android.content.Intent对象。</p> </div><p>根据选项,构造一个意图Intent对象。</p>
<p>例如:</p> <p>例如:</p>
<pre><code>var i = app.intent({ <pre><code>//打开应用来查看图片文件
action: &quot;android.intent.action.VIEW&quot;, var i = app.intent({
type: &quot;text/plain&quot;, action: &quot;VIEW&quot;,
data: &quot;file:///sdcard/1.txt&quot;, type: &quot;image/png&quot;,
data: &quot;file:///sdcard/1.png&quot;
}); });
</code></pre><p>如果你看了一脸懵逼,请百度<a href="https://www.baidu.com/s?wd=android%20Intent">安卓Intent</a></p> app.startAcvitity(i);
<h2>app.startActivity(intent)<span><a class="mark" href="#app_app_startactivity_intent" id="app_app_startactivity_intent">#</a></span></h2> </code></pre><p>更多信息,请百度<a href="https://www.baidu.com/s?wd=android%20Intent">安卓Intent</a>或参考<a href="https://developer.android.com/guide/components/intents-filters.html#Types">Android指南: Intent</a></p>
<h2>app.startActivity(options)<span><a class="mark" href="#app_app_startactivity_options" id="app_app_startactivity_options">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>intent <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 与app.intent函数的参数一样的intent对象。</li> <li>options <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 选项</li>
</ul> </ul>
</div><p>相当于context.startActivity(intent)</p> </div><p>根据选项构造一个Intent并启动该Activity</p>
<h2>app.sendBroadcast(intent)<span><a class="mark" href="#app_app_sendbroadcast_intent" id="app_app_sendbroadcast_intent">#</a></span></h2> <h2>app.sendBroadcast(options)<span><a class="mark" href="#app_app_sendbroadcast_options" id="app_app_sendbroadcast_options">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>intent <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 与app.intent函数的参数一样的intent对象。</li> <li>options <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 选项</li>
</ul> </ul>
</div><p>相当于context.sendBroadcast(intent)</p> </div><p>根据选项构造一个Intent并发送该广播</p>
</div> </div>
</div> </div>

View File

@@ -114,6 +114,7 @@ console.log(&#39;count: %d&#39;, count);
console.log(&#39;count:&#39;, count); console.log(&#39;count:&#39;, count);
// 打印: count: 5 到 stdout // 打印: count: 5 到 stdout
</code></pre><p>详见 util.format()。</p> </code></pre><p>详见 util.format()。</p>
<p>该函数也可以作为全局函数使用。</p>
<h2>console.verbose([data][, ...args])<span><a class="mark" href="#console_console_verbose_data_args" id="console_console_verbose_data_args">#</a></span></h2> <h2>console.verbose([data][, ...args])<span><a class="mark" href="#console_console_verbose_data_args" id="console_console_verbose_data_args">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>data</code> <any></li> <li><code>data</code> <any></li>
@@ -144,13 +145,15 @@ console.log(&#39;count:&#39;, count);
<li>message <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> value为false时要输出的信息</li> <li>message <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> value为false时要输出的信息</li>
</ul> </ul>
</div><p>断言。如果value为false则输出错误信息message并停止脚本运行。</p> </div><p>断言。如果value为false则输出错误信息message并停止脚本运行。</p>
<h2>console.input(data[, ...args])<span><a class="mark" href="#console_console_input_data_args" id="console_console_input_data_args">#</a></span></h2> <pre><code>var a = 1 + 1;
console.assert(a == 2, &quot;加法出错啦&quot;);
</code></pre><h2>console.input(data[, ...args])<span><a class="mark" href="#console_console_input_data_args" id="console_console_input_data_args">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>data</code> <any></li> <li><code>data</code> <any></li>
<li><code>...args</code> <any></li> <li><code>...args</code> <any></li>
</ul> </ul>
</div><p>与console.log一样输出信息并在控制台显示输入框等待输入。按控制台的确认按钮后会将输入的字符串用eval计算后返回。</p> </div><p>与console.log一样输出信息并在控制台显示输入框等待输入。按控制台的确认按钮后会将输入的字符串用eval计算后返回。</p>
<p>部分机型可能会有控制台不显示输入框的情况属于bug。</p> <p><strong>部分机型可能会有控制台不显示输入框的情况属于bug。</strong></p>
<p>例如:</p> <p>例如:</p>
<pre><code>var n = console.input(&quot;请输入一个数字:&quot;); <pre><code>var n = console.input(&quot;请输入一个数字:&quot;);
//输入123之后 //输入123之后
@@ -164,7 +167,7 @@ toast(n + 1);
</div><p>与console.log一样输出信息并在控制台显示输入框等待输入。按控制台的确认按钮后会将输入的字符串直接返回。</p> </div><p>与console.log一样输出信息并在控制台显示输入框等待输入。按控制台的确认按钮后会将输入的字符串直接返回。</p>
<p>部分机型可能会有控制台不显示输入框的情况属于bug。</p> <p>部分机型可能会有控制台不显示输入框的情况属于bug。</p>
<p>例如:</p> <p>例如:</p>
<pre><code>var n = console.input(&quot;请输入一个数字:&quot;); <pre><code>var n = console.rawInput(&quot;请输入一个数字:&quot;);
//输入123之后 //输入123之后
toast(n + 1); toast(n + 1);
//显示1231 //显示1231
@@ -174,17 +177,22 @@ toast(n + 1);
<li><code>h</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 高度</li> <li><code>h</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 高度</li>
</ul> </ul>
</div><p>设置控制台的大小,单位像素。</p> </div><p>设置控制台的大小,单位像素。</p>
<h2>console.setPosition(x, y)<span><a class="mark" href="#console_console_setposition_x_y" id="console_console_setposition_x_y">#</a></span></h2> <pre><code>console.show();
//设置控制台大小为屏幕的四分之一
console.setSize(device.width / 2, device.height / 2);
</code></pre><h2>console.setPosition(x, y)<span><a class="mark" href="#console_console_setposition_x_y" id="console_console_setposition_x_y">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
</ul> </ul>
</div><p>设置控制台的位置,单位像素。</p> </div><p>设置控制台的位置,单位像素。</p>
<h2>print(text)<span><a class="mark" href="#console_print_text" id="console_print_text">#</a></span></h2> <pre><code>console.show();
console.setPosition(100, 100);
</code></pre><h2>print(text)<span><a class="mark" href="#console_print_text" id="console_print_text">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>text <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 要打印到控制台的信息</li> <li>text <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 要打印到控制台的信息</li>
</ul> </ul>
</div><p>在控制台中输出文本text。不会自动换行</p> </div><p>相当于<code>log(text)</code></p>
</div> </div>
</div> </div>

View File

@@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Android7.0以上点按与手势模拟 | Auto.js 3.0.0 文档</title> <title>基于坐标的触摸模拟 | Auto.js 3.0.0 文档</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/sh.css"> <link rel="stylesheet" href="assets/sh.css">
@@ -71,14 +71,17 @@
<div id="toc"> <div id="toc">
<h2>目录</h2> <h2>目录</h2>
<ul> <ul>
<li><span class="stability_2"><a href="#coordinates_based_automation_android7_0">Android7.0以上点按与手势模拟</a></span><ul> <li><span class="stability_2"><a href="#coordinates_based_automation">基于坐标的触摸模拟</a></span><ul>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_setscreenmetrics_width_height">setScreenMetrics(width, height)</a></span></li>
</ul>
</li>
<li><span class="stability_2"><a href="#coordinates_based_automation_7_0">安卓7.0以上的触摸和手势模拟</a></span><ul>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_click_x_y">click(x, y)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_click_x_y">click(x, y)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_longclick_x_y">longClick(x, y)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_longclick_x_y">longClick(x, y)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_press_x_y_duration">press(x, y, duration)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_press_x_y_duration">press(x, y, duration)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_swipe_x1_y1_x2_y2_duration">swipe(x1, y1, x2, y2, duration)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_swipe_x1_y1_x2_y2_duration">swipe(x1, y1, x2, y2, duration)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_gesture_duration_x1_y1_x2_y2">gesture(duration, [x1, y1], [x2, y2], ...)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_gesture_duration_x1_y1_x2_y2">gesture(duration, [x1, y1], [x2, y2], ...)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_gestures_delay1_duration1_x1_y1_x2_y2_delay2_duration2_x3_y3_x4_y4">gestures([delay1, duration1, [x1, y1], [x2, y2], ...], [delay2, duration2, [x3, y3], [x4, y4], ...], ...)</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_gestures_delay1_duration1_x1_y1_x2_y2_delay2_duration2_x3_y3_x4_y4">gestures([delay1, duration1, [x1, y1], [x2, y2], ...], [delay2, duration2, [x3, y3], [x4, y4], ...], ...)</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_setscreenmetrics_width_height">setScreenMetrics(width, height)</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_2"><a href="#coordinates_based_automation_rootautomator">RootAutomator</a></span><ul> <li><span class="stability_2"><a href="#coordinates_based_automation_rootautomator">RootAutomator</a></span><ul>
@@ -86,8 +89,8 @@
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id">RootAutomator.swipe(x1, x2, y1, y2[, duration, id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id">RootAutomator.swipe(x1, x2, y1, y2[, duration, id])</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_press_x_y_duration_id">RootAutomator.press(x, y, duration[, id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_press_x_y_duration_id">RootAutomator.press(x, y, duration[, id])</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_longpress_x_y_id">RootAutomator.longPress(x, y[\, id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_longpress_x_y_id">RootAutomator.longPress(x, y[\, id])</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchdown_x_y_id">RootAutomator.touchDown(x, y[\, id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchdown_x_y_id">RootAutomator.touchDown(x, y[, id])</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchmove_x_y_id">RootAutomator.touchMove(x, y[\, id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchmove_x_y_id">RootAutomator.touchMove(x, y[, id])</a></span></li>
<li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchup_id">RootAutomator.touchUp([id])</a></span></li> <li><span class="stability_undefined"><a href="#coordinates_based_automation_rootautomator_touchup_id">RootAutomator.touchUp([id])</a></span></li>
</ul> </ul>
</li> </li>
@@ -101,9 +104,29 @@
</div> </div>
<div id="apicontent"> <div id="apicontent">
<h1>Android7.0以上点按与手势模拟<span><a class="mark" href="#coordinates_based_automation_android7_0" id="coordinates_based_automation_android7_0">#</a></span></h1> <h1>基于坐标的触摸模拟<span><a class="mark" href="#coordinates_based_automation" id="coordinates_based_automation">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>本章节介绍了一些适用于Android7.0以上、不需要root权限、依赖于无障碍服务的点按与手势模拟的全局函数</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>本章节介绍了一些使用坐标进行点击、滑动的函数。这些函数有的需要安卓7.0以上,有的需要root权限。</p>
<p><strong>注意以下命令只有Android7.0及以上才有效</strong></p> <p>要获取要点击的位置的坐标,可以在开发者选项中开启&quot;指针位置&quot;</p>
<p>基于坐标的脚本通常会有分辨率的问题,这时可以通过<code>setScreenMetrics()</code>函数来进行自动坐标放缩。这个函数会影响本章节的所有点击、长按、滑动等函数。通过设定脚本设计时的分辨率,使得脚本在其他分辨率下自动放缩坐标。</p>
<p>控件和坐标也可以相互结合。一些控件是无法点击的(clickable为false), 无法通过<code>.click()</code>函数来点击这时如果安卓版本在7.0以上或者有root权限就可以通过以下方式来点击</p>
<pre><code>//获取这个控件
var widget = id(&quot;xxx&quot;).findOne();
//获取其中心位置并点击
click(widget.bounds().centerX(), widget.bounds().centerY());
//如果用root权限则用Tap
</code></pre><h2>setScreenMetrics(width, height)<span><a class="mark" href="#coordinates_based_automation_setscreenmetrics_width_height" id="coordinates_based_automation_setscreenmetrics_width_height">#</a></span></h2>
<div class="signature"><ul>
<li>width <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 屏幕宽度,单位像素</li>
<li>height <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 屏幕高度,单位像素</li>
</ul>
</div><p>设置脚本坐标点击所适合的屏幕宽高。如果脚本运行时,屏幕宽度不一致会自动放缩坐标。</p>
<p>例如在1920*1080的设备中某个操作的代码为</p>
<pre><code>setScreenMetrics(1080, 1920);
click(800, 200);
longClick(300, 500);
</code></pre><p>那么在其他设备上AutoJs会自动放缩坐标以便脚本仍然有效。例如在540 * 960的屏幕中<code>click(800, 200)</code>实际上会点击位置(400, 100)。</p>
<h1>安卓7.0以上的触摸和手势模拟<span><a class="mark" href="#coordinates_based_automation_7_0" id="coordinates_based_automation_7_0">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p><strong>注意以下命令只有Android7.0及以上才有效</strong></p>
<h2>click(x, y)<span><a class="mark" href="#coordinates_based_automation_click_x_y" id="coordinates_based_automation_click_x_y">#</a></span></h2> <h2>click(x, y)<span><a class="mark" href="#coordinates_based_automation_click_x_y" id="coordinates_based_automation_click_x_y">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要点击的坐标的x值</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要点击的坐标的x值</li>
@@ -111,16 +134,13 @@
</ul> </ul>
</div><p>模拟点击坐标(x, y),并返回是否点击成功。只有在点击执行完成后脚本才继续执行。</p> </div><p>模拟点击坐标(x, y),并返回是否点击成功。只有在点击执行完成后脚本才继续执行。</p>
<p>一般而言,只有点击过程(大约150毫秒)中被其他事件中断(例如用户自行点击)才会点击失败。</p> <p>一般而言,只有点击过程(大约150毫秒)中被其他事件中断(例如用户自行点击)才会点击失败。</p>
<p>使用该函数模拟连续点击时可能有点击速度过慢的问题,这时可以用[press][]函数代替。</p> <p>使用该函数模拟连续点击时可能有点击速度过慢的问题,这时可以用<code>press()</code>函数代替。</p>
<blockquote>
<p>可以在开发者选项中启用指针位置来查看坐标</p>
</blockquote>
<h2>longClick(x, y)<span><a class="mark" href="#coordinates_based_automation_longclick_x_y" id="coordinates_based_automation_longclick_x_y">#</a></span></h2> <h2>longClick(x, y)<span><a class="mark" href="#coordinates_based_automation_longclick_x_y" id="coordinates_based_automation_longclick_x_y">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要长按的坐标的x值</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要长按的坐标的x值</li>
<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要长按的坐标的y值</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 要长按的坐标的y值</li>
</ul> </ul>
</div><p>模拟长按坐标(x, y), 并 返回是否成功。只有在长按执行完成大约600毫秒时脚本才会继续执行。</p> </div><p>模拟长按坐标(x, y), 并返回是否成功。只有在长按执行完成大约600毫秒时脚本才会继续执行。</p>
<p>一般而言,只有长按过程中被其他事件中断(例如用户自行点击)才会长按失败。</p> <p>一般而言,只有长按过程中被其他事件中断(例如用户自行点击)才会长按失败。</p>
<h2>press(x, y, duration)<span><a class="mark" href="#coordinates_based_automation_press_x_y_duration" id="coordinates_based_automation_press_x_y_duration">#</a></span></h2> <h2>press(x, y, duration)<span><a class="mark" href="#coordinates_based_automation_press_x_y_duration" id="coordinates_based_automation_press_x_y_duration">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
@@ -131,7 +151,13 @@
</div><p>模拟按住坐标(x, y), 并返回是否成功。只有按住操作执行完成时脚本才会继续执行。</p> </div><p>模拟按住坐标(x, y), 并返回是否成功。只有按住操作执行完成时脚本才会继续执行。</p>
<p>如果按住时间过短那么会被系统认为是点击如果时长超过500毫秒则认为是长按。</p> <p>如果按住时间过短那么会被系统认为是点击如果时长超过500毫秒则认为是长按。</p>
<p>一般而言,只有按住过程中被其他事件中断才会操作失败。</p> <p>一般而言,只有按住过程中被其他事件中断才会操作失败。</p>
<h2>swipe(x1, y1, x2, y2, duration)<span><a class="mark" href="#coordinates_based_automation_swipe_x1_y1_x2_y2_duration" id="coordinates_based_automation_swipe_x1_y1_x2_y2_duration">#</a></span></h2> <p>一个连点器的例子如下:</p>
<pre><code>//循环100次
for(var i = 0; i &lt; 100; i++){
//点击位置(500, 1000), 每次用时1毫秒
press(500, 1000, 1);
}
</code></pre><h2>swipe(x1, y1, x2, y2, duration)<span><a class="mark" href="#coordinates_based_automation_swipe_x1_y1_x2_y2_duration" id="coordinates_based_automation_swipe_x1_y1_x2_y2_duration">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动的起始坐标的x值</li> <li><code>x1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动的起始坐标的x值</li>
<li><code>y1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动的起始坐标的y值</li> <li><code>y1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动的起始坐标的y值</li>
@@ -152,22 +178,17 @@
<p>例如手指捏合:</p> <p>例如手指捏合:</p>
<pre><code>gestures([0, 500, [800, 300], [500, 1000]], <pre><code>gestures([0, 500, [800, 300], [500, 1000]],
[0, 500, [300, 1500], [500, 1000]]); [0, 500, [300, 1500], [500, 1000]]);
</code></pre><h2>setScreenMetrics(width, height)<span><a class="mark" href="#coordinates_based_automation_setscreenmetrics_width_height" id="coordinates_based_automation_setscreenmetrics_width_height">#</a></span></h2> </code></pre><h1>RootAutomator<span><a class="mark" href="#coordinates_based_automation_rootautomator" id="coordinates_based_automation_rootautomator">#</a></span></h1>
<div class="signature"><ul>
<li>width <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 屏幕宽度,单位像素</li>
<li>height <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 屏幕高度,单位像素</li>
</ul>
</div><p>设置脚本坐标点击所适合的屏幕宽高。如果脚本运行时,屏幕宽度不一致会自动放缩坐标。</p>
<p>例如在1920*1080的设备中某个操作的代码为</p>
<pre><code>setScreenMetrics(1080, 1920);
click(800, 200);
longClick(300, 500);
</code></pre><p>那么在其他设备上AutoJs会自动放缩坐标以便脚本仍然有效。</p>
<h1>RootAutomator<span><a class="mark" href="#coordinates_based_automation_rootautomator" id="coordinates_based_automation_rootautomator">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>RootAutomator是一个使用root权限来模拟触摸的对象用它可以完成触摸与多点触摸并且这些动作的执行没有延迟。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>RootAutomator是一个使用root权限来模拟触摸的对象用它可以完成触摸与多点触摸并且这些动作的执行没有延迟。</p>
<p><strong>注意以下命令需要root权限</strong></p> <p>一个脚本中最好只存在一个RootAutomator并且保证脚本结束退出他。可以在exit事件中退出RootAutomator例如</p>
<pre><code>var ra = new RootAutomator(); <pre><code>var ra = new RootAutomator();
</code></pre><h2>RootAutomator.tap(x, y[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_tap_x_y_id" id="coordinates_based_automation_rootautomator_tap_x_y_id">#</a></span></h2> events.on(&#39;exit&#39;, function(){
ra.exit();
});
//执行一些点击操作
...
</code></pre><p><strong>注意以下命令需要root权限</strong></p>
<h2>RootAutomator.tap(x, y[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_tap_x_y_id" id="coordinates_based_automation_rootautomator_tap_x_y_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
@@ -182,6 +203,7 @@ ra.tap(200, 200, 2);
ra.exit(); ra.exit();
</code></pre><p>如果不需要多点触摸则不需要id这个参数。 </code></pre><p>如果不需要多点触摸则不需要id这个参数。
多点触摸通常用于手势或游戏操作,例如模拟双指捏合、双指上滑等。</p> 多点触摸通常用于手势或游戏操作,例如模拟双指捏合、双指上滑等。</p>
<p>某些情况下可能存在tap点击无反应的情况这时可以用<code>RootAutomator.press()</code>函数代替。</p>
<h2>RootAutomator.swipe(x1, x2, y1, y2[, duration, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id" id="coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id">#</a></span></h2> <h2>RootAutomator.swipe(x1, x2, y1, y2[, duration, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id" id="coordinates_based_automation_rootautomator_swipe_x1_x2_y1_y2_duration_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>x1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动起点横坐标</li> <li><code>x1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 滑动起点横坐标</li>
@@ -194,43 +216,42 @@ ra.exit();
</div><p>模拟一次从(x1, y1)到(x2, y2)的时间为duration毫秒的滑动。</p> </div><p>模拟一次从(x1, y1)到(x2, y2)的时间为duration毫秒的滑动。</p>
<h2>RootAutomator.press(x, y, duration[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_press_x_y_duration_id" id="coordinates_based_automation_rootautomator_press_x_y_duration_id">#</a></span></h2> <h2>RootAutomator.press(x, y, duration[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_press_x_y_duration_id" id="coordinates_based_automation_rootautomator_press_x_y_duration_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>x <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li>y <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
<li>duration <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 按下时长</li> <li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 按下时长</li>
<li>id <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li>
</ul> </ul>
</div><p>模拟按下位置(x, y)时长为duration毫秒。</p> </div><p>模拟按下位置(x, y)时长为duration毫秒。</p>
<p>使用该函数模拟连续点击时可能有点击速度过慢的问题,这时可以用[RootAutomator.press][]函数代替。</p>
<h2>RootAutomator.longPress(x, y[\, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_longpress_x_y_id" id="coordinates_based_automation_rootautomator_longpress_x_y_id">#</a></span></h2> <h2>RootAutomator.longPress(x, y[\, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_longpress_x_y_id" id="coordinates_based_automation_rootautomator_longpress_x_y_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>x <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li>y <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
<li>duration <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 按下时长</li> <li><code>duration</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 按下时长</li>
<li>id <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li>
</ul> </ul>
</div><p>模拟长按位置(x, y)。</p> </div><p>模拟长按位置(x, y)。</p>
<p>以上为简单模拟触摸操作的函数。如果要模拟一些复杂的手势,需要更底层的函数。</p> <p>以上为简单模拟触摸操作的函数。如果要模拟一些复杂的手势,需要更底层的函数。</p>
<h2>RootAutomator.touchDown(x, y[\, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchdown_x_y_id" id="coordinates_based_automation_rootautomator_touchdown_x_y_id">#</a></span></h2> <h2>RootAutomator.touchDown(x, y[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchdown_x_y_id" id="coordinates_based_automation_rootautomator_touchdown_x_y_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>x <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li>y <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
<li>id <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li>
</ul> </ul>
</div><p>模拟手指按下位置(x, y)。</p> </div><p>模拟手指按下位置(x, y)。</p>
<h2>RootAutomator.touchMove(x, y[\, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchmove_x_y_id" id="coordinates_based_automation_rootautomator_touchmove_x_y_id">#</a></span></h2> <h2>RootAutomator.touchMove(x, y[, id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchmove_x_y_id" id="coordinates_based_automation_rootautomator_touchmove_x_y_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>x <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li> <li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 横坐标</li>
<li>y <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li> <li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 纵坐标</li>
<li>id <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li>
</ul> </ul>
</div><p>模拟移动手指到位置(x, y)。</p> </div><p>模拟移动手指到位置(x, y)。</p>
<h2>RootAutomator.touchUp([id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchup_id" id="coordinates_based_automation_rootautomator_touchup_id">#</a></span></h2> <h2>RootAutomator.touchUp([id])<span><a class="mark" href="#coordinates_based_automation_rootautomator_touchup_id" id="coordinates_based_automation_rootautomator_touchup_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>id <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 多点触摸id可选默认为1</li>
</ul> </ul>
</div><p>模拟手指弹起。</p> </div><p>模拟手指弹起。</p>
<h1>使用root权限点击和滑动的简单命令<span><a class="mark" href="#coordinates_based_automation_root" id="coordinates_based_automation_root">#</a></span></h1> <h1>使用root权限点击和滑动的简单命令<span><a class="mark" href="#coordinates_based_automation_root" id="coordinates_based_automation_root">#</a></span></h1>
<div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental </div><p>注意:本章节的函数在后续版本很可能有改动!请勿过分依赖本章节函数的副作用。推荐使用[RootAutomator][]代替本章节的触摸函数。</p> <div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental </div><p>注意:本章节的函数在后续版本很可能有改动!请勿过分依赖本章节函数的副作用。推荐使用<code>RootAutomator</code>代替本章节的触摸函数。</p>
<p>以下函数均需要root权限可以实现任意位置的点击、滑动等。</p> <p>以下函数均需要root权限可以实现任意位置的点击、滑动等。</p>
<ul> <ul>
<li>这些函数通常首字母大写以表示其特殊的权限。 </li> <li>这些函数通常首字母大写以表示其特殊的权限。 </li>

View File

@@ -119,17 +119,17 @@
<div id="apicontent"> <div id="apicontent">
<h1>Device<span><a class="mark" href="#device_device" id="device_device">#</a></span></h1> <h1>Device<span><a class="mark" href="#device_device" id="device_device">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>device模块提供了与设备有关的信息与操作例如获取设备宽高内存使用率IMEI调整设备亮度、音量等。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>device模块提供了与设备有关的信息与操作例如获取设备宽高内存使用率IMEI调整设备亮度、音量等。</p>
<p>此模块的部分函数,例如调整音量,需要&quot;修改系统设置&quot;的权限。如果没有该权限,会抛出异常并跳转到权限设置界面。</p> <p>此模块的部分函数,例如调整音量,需要&quot;修改系统设置&quot;的权限。如果没有该权限,会抛出<code>SecurityException</code>并跳转到权限设置界面。</p>
<h2>device.width<span><a class="mark" href="#device_device_width" id="device_device_width">#</a></span></h2> <h2>device.width<span><a class="mark" href="#device_device_width" id="device_device_width">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul> </ul>
</div><p>设备宽度。例如1080。</p> </div><p>设备屏幕分辨率宽度。例如1080。</p>
<h2>device.height<span><a class="mark" href="#device_device_height" id="device_device_height">#</a></span></h2> <h2>device.height<span><a class="mark" href="#device_device_height" id="device_device_height">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul> </ul>
</div><p>设备高度。例如1920。</p> </div><p>设备屏幕分辨率高度。例如1920。</p>
<h2>device.buildId<span><a class="mark" href="#device_device_buildid" id="device_device_buildid">#</a></span></h2> <h2>device.buildId<span><a class="mark" href="#device_device_buildid" id="device_device_buildid">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>

View File

@@ -72,14 +72,14 @@
<h2>目录</h2> <h2>目录</h2>
<ul> <ul>
<li><span class="stability_2"><a href="#dialogs_dialogs">Dialogs</a></span><ul> <li><span class="stability_2"><a href="#dialogs_dialogs">Dialogs</a></span><ul>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_rawinput_title_default">dialogs.rawInput(title[, default])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_alert_title_content_callback">dialogs.alert(title[, content, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_input_title_default">dialogs.input(title[, default])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_confirm_title_content_callback">dialogs.confirm(title[, content, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_prompt_title_default">dialogs.prompt(title[, default])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_rawinput_title_prefill_callback">dialogs.rawInput(title[, prefill, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_alert_title_content">dialogs.alert(title[, content])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_input_title_prefill_callback">dialogs.input(title[, prefill, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_confirm_title_content">dialogs.confirm(title[, content])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_prompt_title_prefill_callback">dialogs.prompt(title[, prefill, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_select_title_items">dialogs.select(title, items)</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_select_title_items_callback">dialogs.select(title, items, callback)</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_singlechoice_title_items_index">dialogs.singleChoice(title, items[, index])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_singlechoice_title_items_index_callback">dialogs.singleChoice(title, items[, index, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#dialogs_dialogs_multichoice_title_items_indexes">dialogs.multiChoice(title, items[, indexes])</a></span></li> <li><span class="stability_undefined"><a href="#dialogs_dialogs_multichoice_title_items_indices_callback">dialogs.multiChoice(title, items[, indices, callback])</a></span></li>
</ul> </ul>
</li> </li>
</ul> </ul>
@@ -88,56 +88,128 @@
<div id="apicontent"> <div id="apicontent">
<h1>Dialogs<span><a class="mark" href="#dialogs_dialogs" id="dialogs_dialogs">#</a></span></h1> <h1>Dialogs<span><a class="mark" href="#dialogs_dialogs" id="dialogs_dialogs">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>dialogs 模块允许用户通过对话框与脚本进行交互。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>dialogs 模块提供了简单的对话框支持,可以通过对话框和用户进行交互。最简单的例子如下:</p>
<h2>dialogs.rawInput(title[, default])<span><a class="mark" href="#dialogs_dialogs_rawinput_title_default" id="dialogs_dialogs_rawinput_title_default">#</a></span></h2> <pre><code>alert(&quot;您好&quot;);
<div class="signature"><ul> </code></pre><p>这段代码会弹出一个消息提示框显示&quot;您好&quot;,并在用户点击&quot;确定&quot;后继续运行。稍微复杂一点的例子如下:</p>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <pre><code>var clear = confirm(&quot;要清除所有缓存吗?&quot;);
<li><code>default</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 可选,对话框内输入框的初始内容。默认为 <code>&quot;&quot;</code></li> if(clear){
</ul> alert(&quot;清除成功!&quot;);
</div><p>显示一个包含输入框的对话框。如果指定了 <code>default</code> ,那么对话框显示时输入框的内容就为该值。</p> }
<p>该函数也可通过 <code>rawInput(title[, default])</code><code>prompt(title[, default])</code> 或者 <code>dialogs.prompt(title[, default])</code> 来调用。调用时脚本将阻塞直至对话框被关闭。如果用户输入内容并点击确定,函数将返回输入的内容;否则返回 <code>null</code> </p> </code></pre><p><code>confirm()</code>会弹出一个对话框并让用户选择&quot;&quot;&quot;&quot;,如果选择&quot;&quot;则返回true</p>
<h2>dialogs.input(title[, default])<span><a class="mark" href="#dialogs_dialogs_input_title_default" id="dialogs_dialogs_input_title_default">#</a></span></h2> <p>需要特别注意的是对话框在ui模式下不能像通常那样使用应该使用回调函数或者<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>的形式。理解这一点可能稍有困难。举个例子:</p>
<p>等效于 <code>eval(dialogs.rawInput(title, default))</code> </p> <pre><code>&quot;ui&quot;;
<h2>dialogs.prompt(title[, default])<span><a class="mark" href="#dialogs_dialogs_prompt_title_default" id="dialogs_dialogs_prompt_title_default">#</a></span></h2> //回调形式
<p>见 dialogs.rawInput</p> confirm(&quot;要清除所有缓存吗?&quot;, function(clear){
<h2>dialogs.alert(title[, content])<span><a class="mark" href="#dialogs_dialogs_alert_title_content" id="dialogs_dialogs_alert_title_content">#</a></span></h2> if(clear){
alert(&quot;清除成功!&quot;);
}
});
//Promise形式
confirm(&quot;要清除所有缓存吗?&quot;)
.then(clear =&gt; {
if(clear){
alert(&quot;清除成功!&quot;);
}
});
</code></pre><h2>dialogs.alert(title[, content, callback])<span><a class="mark" href="#dialogs_dialogs_alert_title_content_callback" id="dialogs_dialogs_alert_title_content_callback">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>content</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 可选,对话框的内容。默认为空。</li> <li><code>content</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 可选,对话框的内容。默认为空。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul> </ul>
</div><p>显示一个只包含“确定”按钮的提示对话框。</p> </div><p>显示一个只包含“确定”按钮的提示对话框。直至用户点击确定脚本才继续运行。</p>
<p>该函数也可通过 <code>alert(title[, content])</code> 来调用。调用时脚本将阻塞直至对话框被关闭。该函数无返回值</p> <p>该函数也可以作为全局函数使用</p>
<h2>dialogs.confirm(title[, content])<span><a class="mark" href="#dialogs_dialogs_confirm_title_content" id="dialogs_dialogs_confirm_title_content">#</a></span></h2> <pre><code>alert(&quot;出现错误~&quot;, &quot;出现未知错误,请联系脚本作者”);
</code></pre><p>在ui模式下该函数返回一个<code>Promise</code>。例如:</p>
<pre><code>&quot;ui&quot;;
alert(&quot;嘿嘿嘿&quot;).then(()=&gt;{
//当点击确定后会执行这里
});
</code></pre><h2>dialogs.confirm(title[, content, callback])<span><a class="mark" href="#dialogs_dialogs_confirm_title_content_callback" id="dialogs_dialogs_confirm_title_content_callback">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>content</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 可选,对话框的内容。默认为空。</li> <li><code>content</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 可选,对话框的内容。默认为空。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul> </ul>
</div><p>显示一个包含“确定”和“取消”按钮的提示对话框。</p> </div><p>显示一个包含“确定”和“取消”按钮的提示对话框。如果用户点击“确定”则返回 <code>true</code> ,否则返回 <code>false</code></p>
<p>该函数也可通过 <code>confirm(title[, content])</code> 来调用。调用时脚本将阻塞直至对话框被关闭。如果用户点击“确定”则返回 <code>true</code> ,否则返回 <code>false</code> </p> <p>该函数也可以作为全局函数使用</p>
<h2>dialogs.select(title, items)<span><a class="mark" href="#dialogs_dialogs_select_title_items" id="dialogs_dialogs_select_title_items">#</a></span></h2> <p>在ui模式下该函数返回一个<code>Promise</code>。例如:</p>
<pre><code>&quot;ui&quot;;
confirm(&quot;确定吗&quot;).then(value=&gt;{
//当点击确定后会执行这里, value为true或false, 表示点击&quot;确定&quot;&quot;取消&quot;
});
</code></pre><h2>dialogs.rawInput(title[, prefill, callback])<span><a class="mark" href="#dialogs_dialogs_rawinput_title_prefill_callback" id="dialogs_dialogs_rawinput_title_prefill_callback">#</a></span></h2>
<div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>prefill</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 输入框的初始内容,可选,默认为空。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul>
</div><p>显示一个包含输入框的对话框等待用户输入内容并在用户点击确定时将输入的字符串返回。如果用户取消了输入返回null。</p>
<p>该函数也可以作为全局函数使用。</p>
<pre><code>var name = rawInput(&quot;请输入您的名字&quot;, &quot;小明&quot;);
alert(&quot;您的名字是&quot; + name);
</code></pre><p>在ui模式下该函数返回一个<code>Promise</code>。例如:</p>
<pre><code>&quot;ui&quot;;
rawInput(&quot;请输入您的名字&quot;, &quot;小明&quot;).then(name =&gt; {
alert(&quot;您的名字是&quot; + name);
});
</code></pre><p>当然也可以使用回调函数,例如:</p>
<pre><code>rawInput(&quot;请输入您的名字&quot;, &quot;小明&quot;, name =&gt; {
alert(&quot;您的名字是&quot; + name);
});
</code></pre><h2>dialogs.input(title[, prefill, callback])<span><a class="mark" href="#dialogs_dialogs_input_title_prefill_callback" id="dialogs_dialogs_input_title_prefill_callback">#</a></span></h2>
<p>等效于 <code>eval(dialogs.rawInput(title, prefill, callback))</code>, 该函数和rawInput的区别在于会把输入的字符串用eval计算一遍再返回返回的可能不是字符串。</p>
<p>可以用该函数输入数字、数组等。例如:</p>
<pre><code>var age = dialogs.input(&quot;请输入您的年龄&quot;, &quot;18&quot;);
// new Date().getYear() + 1900 可获取当前年份
var year = new Date().getYear() + 1900 - age;
alert(&quot;您的出生年份是&quot; + year);
</code></pre><p>在ui模式下该函数返回一个<code>Promise</code>。例如:</p>
<pre><code>&quot;ui&quot;;
dialogs.input(&quot;请输入您的年龄&quot;, &quot;18&quot;).then(age =&gt; {
var year = new Date().getYear() + 1900 - age;
alert(&quot;您的出生年份是&quot; + year);
});
</code></pre><h2>dialogs.prompt(title[, prefill, callback])<span><a class="mark" href="#dialogs_dialogs_prompt_title_prefill_callback" id="dialogs_dialogs_prompt_title_prefill_callback">#</a></span></h2>
<p>相当于 <code>dialogs.rawInput()</code>;</p>
<h2>dialogs.select(title, items, callback)<span><a class="mark" href="#dialogs_dialogs_select_title_items_callback" id="dialogs_dialogs_select_title_items_callback">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li> <li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul> </ul>
</div><p>显示一个带有选项列表的对话框。</p> </div><p>显示一个带有选项列表的对话框,等待用户选择,返回用户选择的选项索引(0 ~ item.length - 1)。如果用户取消了选择,返回-1</p>
<p>该函数也可通过 <code>dialogs.select(title, ...items)</code> 来调用。<br>例如: <code>dialogs.select(&quot;标题&quot;, [&quot;A&quot;, &quot;B&quot;, &quot;C&quot;])</code> 可以用 <code>dialogs.select(&quot;标题&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;)</code> 替代。</p> <pre><code>var options = [&quot;选项A&quot;, &quot;选项B&quot;, &quot;选项C&quot;, &quot;选项D&quot;]
<p>调用时脚本将阻塞直至对话框被关闭。如果用户点击了对话框中的某个选项该函数会返回该选项的位置选中第一个选项返回0第二个选项返回1以此类推否则返回-1。</p> var i = dialogs.select(&quot;请选择一个选项&quot;, options);
<h2>dialogs.singleChoice(title, items[, index])<span><a class="mark" href="#dialogs_dialogs_singlechoice_title_items_index" id="dialogs_dialogs_singlechoice_title_items_index">#</a></span></h2> if(i &gt;= 0){
toast(&quot;您选择的是&quot; + options[i]);
}else{
toast(&quot;您取消了选择&quot;);
}
</code></pre><p>在ui模式下该函数返回一个<code>Promise</code>。例如:</p>
<pre><code>&quot;ui&quot;;
dialogs.select(&quot;请选择一个选项&quot;, [&quot;选项A&quot;, &quot;选项B&quot;, &quot;选项C&quot;, &quot;选项D&quot;])
.then(i =&gt; {
toast(i);
});
</code></pre><h2>dialogs.singleChoice(title, items[, index, callback])<span><a class="mark" href="#dialogs_dialogs_singlechoice_title_items_index_callback" id="dialogs_dialogs_singlechoice_title_items_index_callback">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li> <li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li>
<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 可选,对话框的默认选项的位置默认为0。</li> <li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 对话框的初始选项的位置默认为0。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul> </ul>
</div><p>显示一个带有单选框选项列表对话框。</p> </div><p>显示一个单选列表对话框,等待用户选择,返回用户选择的选项索引(0 ~ item.length - 1)。如果用户取消了选择,返回-1</p>
<p>调用时脚本将阻塞直至对话框被关闭。如果用户选中了对话框中的某个选项并点击“确定”,该函数返回该选项的位置选中第一个选项返回0第二个选项返回1以此类推否则返回-1</p> <p>在ui模式下该函数返回一个<code>Promise</code></p>
<h2>dialogs.multiChoice(title, items[, indexes])<span><a class="mark" href="#dialogs_dialogs_multichoice_title_items_indexes" id="dialogs_dialogs_multichoice_title_items_indexes">#</a></span></h2> <h2>dialogs.multiChoice(title, items[, indices, callback])<span><a class="mark" href="#dialogs_dialogs_multichoice_title_items_indices_callback" id="dialogs_dialogs_multichoice_title_items_indices_callback">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li> <li><code>title</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 对话框的标题。</li>
<li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li> <li><code>items</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 对话框的选项列表,是一个字符串数组。</li>
<li><code>indexes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 可选,对话框的默认选项的位置数组默认为空数组。</li> <li><code>indices</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> 选项列表中初始选中的项目索引的数组默认为空数组。</li>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 回调函数,可选。当用户点击确定时被调用,一般用于ui模式。</li>
</ul> </ul>
</div><p>显示一个带有多选框选项列表对话框。</p> </div><p>显示一个多选列表对话框,等待用户选择,返回用户选择的选项索引的数组。如果用户取消了选择,返回<code>[]</code></p>
<p>调用时脚本将阻塞直至对话框被关闭。如果用户点击“确定”按钮,该函数返回所有已选选项的位置组成的数组,否则返回空数组</p> <p>在ui模式下该函数返回一个<code>Promise</code></p>
</div> </div>
</div> </div>

View File

@@ -83,12 +83,11 @@
<li><span class="stability_undefined"><a href="#engines_scriptexecution">ScriptExecution</a></span><ul> <li><span class="stability_undefined"><a href="#engines_scriptexecution">ScriptExecution</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_scriptexecution_getengine">ScriptExecution.getEngine()</a></span></li> <li><span class="stability_undefined"><a href="#engines_scriptexecution_getengine">ScriptExecution.getEngine()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptexecution_getconfig">ScriptExecution.getConfig()</a></span></li> <li><span class="stability_undefined"><a href="#engines_scriptexecution_getconfig">ScriptExecution.getConfig()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptexecution_getsource">ScriptExecution.getSource()</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_undefined"><a href="#engines_scriptengine">ScriptEngine</a></span><ul> <li><span class="stability_undefined"><a href="#engines_scriptengine">ScriptEngine</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_scriptengine_forcestop">ScriptEngine.forceStop()</a></span></li> <li><span class="stability_undefined"><a href="#engines_scriptengine_forcestop">ScriptEngine.forceStop()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptengine_gettag_tagname">ScriptEngine.getTag(tagName)</a></span></li> <li><span class="stability_undefined"><a href="#engines_scriptengine_cwd">ScriptEngine.cwd()</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_undefined"><a href="#engines_scriptconfig">ScriptConfig</a></span><ul> <li><span class="stability_undefined"><a href="#engines_scriptconfig">ScriptConfig</a></span><ul>
@@ -98,99 +97,126 @@
<li><span class="stability_undefined"><a href="#engines_getpath">getPath()</a></span></li> <li><span class="stability_undefined"><a href="#engines_getpath">getPath()</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_undefined"><a href="#engines_scriptsource">ScriptSource</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_getname">getName()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_getenginename">getEngineName()</a></span></li>
</ul>
</li>
</ul> </ul>
</div> </div>
<div id="apicontent"> <div id="apicontent">
<h1>Engines<span><a class="mark" href="#engines_engines" id="engines_engines">#</a></span></h1> <h1>Engines<span><a class="mark" href="#engines_engines" id="engines_engines">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>engines模块包含了一些与脚本引擎有关的函数包括运行其他脚本关闭脚本等。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>engines模块包含了一些与脚本环境、脚本运行、脚本引擎有关的函数,包括运行其他脚本,关闭脚本等。</p>
<h2>engines.execScript(name, script[, config])<span><a class="mark" href="#engines_engines_execscript_name_script_config" id="engines_engines_execscript_name_script_config">#</a></span></h2> <p>例如,获取脚本所在目录:</p>
<pre><code>toast(engines.myEngine().cwd());
</code></pre><h2>engines.execScript(name, script[, config])<span><a class="mark" href="#engines_engines_execscript_name_script_config" id="engines_engines_execscript_name_script_config">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本名称。这个名称和文件名称无关,只是在任务管理中显示的名称。</li> <li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本名称。这个名称和文件名称无关,只是在任务管理中显示的名称。</li>
<li><code>script</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本内容。</li> <li><code>script</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本内容。</li>
<li><code>config</code> \<Object\> 运行配置项<ul> <li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li> <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1</li> <li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li> <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>在新线程中运行脚本script。返回一个<a href="#engines_scriptexecution">ScriptExectuion</a>对象。</p> </div><p>在新的脚本环境中运行脚本script。返回一个<a href="#engines_scriptexecution">ScriptExectuion</a>对象。</p>
<h2>engines.execScriptFile(path[, config])<span><a class="mark" href="#engines_engines_execscriptfile_path_config" id="engines_engines_execscriptfile_path_config">#</a></span></h2> <p>所谓新的脚本环境,指定是,脚本中的变量和原脚本的变量是不共享的,并且,脚本会在新的线程中运行。</p>
<p>最简单的例子如下:</p>
<pre><code>engines.execScript(&quot;hello world&quot;, &quot;toast(&#39;hello world&#39;)&quot;);
</code></pre><p>如果要循环运行,则:</p>
<pre><code>//每隔3秒运行一次脚本循环10次
engines.execScript(&quot;hello world&quot;, &quot;toast(&#39;hello world&#39;)&quot;, {
loopTimes: 10,
interval: 3000
});
</code></pre><p>用字符串来编写脚本非常不方便,可以结合 <code>Function.toString()</code>的方法来执行特定函数:</p>
<pre><code>function helloWorld(){
//注意,这里的变量和脚本主体的变量并不共享
toast(&quot;hello world&quot;);
}
engines.execScript(&quot;hello world&quot;, &quot;helloWorld();\n&quot; + helloWorld.toString());
</code></pre><p>如果要传递变量,则可以把这些封装成一个函数:</p>
<pre><code>function exec(action, args){
args = args || {};
engines.execScript(action.name, action + &quot;(&quot; + JSON.stringify(args) + &quot;);\n&quot; + action.toString());
}
//要执行的函数,是一个简单的加法
function add(args){
toast(args.a + args.b);
}
//在新的脚本环境中执行 1 + 2
exec(add, {a: 1, b:2});
</code></pre><h2>engines.execScriptFile(path[, config])<span><a class="mark" href="#engines_engines_execscriptfile_path_config" id="engines_engines_execscriptfile_path_config">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本路径。</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本路径。</li>
<li><code>config</code> \<Object\> 运行配置项<ul> <li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li> <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1</li> <li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li> <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>在新线程中运行脚本文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p> </div><p>在新的脚本环境中运行脚本文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p>
<h2>engines.execAutoFile(path[, config])<span><a class="mark" href="#engines_engines_execautofile_path_config" id="engines_engines_execautofile_path_config">#</a></span></h2> <pre><code>engines.execScriptFile(&quot;/sdcard/脚本/1.js&quot;);
</code></pre><h2>engines.execAutoFile(path[, config])<span><a class="mark" href="#engines_engines_execautofile_path_config" id="engines_engines_execautofile_path_config">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的录制文件路径。</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的录制文件路径。</li>
<li><code>config</code> \<Object\> 运行配置项<ul> <li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li> <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1</li> <li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li> <li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这路径会用于require时寻找模块文件。</li>
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>在新线程中运行录制文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p> </div><p>在新的脚本环境中运行录制文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p>
<h2>engines.stopAll()<span><a class="mark" href="#engines_engines_stopall" id="engines_engines_stopall">#</a></span></h2> <pre><code>engines.execAutoFile(&quot;/sdcard/脚本/1.auto&quot;);
</code></pre><h2>engines.stopAll()<span><a class="mark" href="#engines_engines_stopall" id="engines_engines_stopall">#</a></span></h2>
<p>停止所有正在运行的脚本。包括当前脚本自身。</p> <p>停止所有正在运行的脚本。包括当前脚本自身。</p>
<h2>engines.stopAllAndToast()<span><a class="mark" href="#engines_engines_stopallandtoast" id="engines_engines_stopallandtoast">#</a></span></h2> <h2>engines.stopAllAndToast()<span><a class="mark" href="#engines_engines_stopallandtoast" id="engines_engines_stopallandtoast">#</a></span></h2>
<p>停止所有正在运行的脚本并显示停止的脚本数量。包括当前脚本自身。</p> <p>停止所有正在运行的脚本并显示停止的脚本数量。包括当前脚本自身。</p>
<h2>engines.myEngine()<span><a class="mark" href="#engines_engines_myengine" id="engines_engines_myengine">#</a></span></h2> <h2>engines.myEngine()<span><a class="mark" href="#engines_engines_myengine" id="engines_engines_myengine">#</a></span></h2>
<p>返回当前脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p> <p>返回当前脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p>
<h1>ScriptExecution<span><a class="mark" href="#engines_scriptexecution" id="engines_scriptexecution">#</a></span></h1> <h1>ScriptExecution<span><a class="mark" href="#engines_scriptexecution" id="engines_scriptexecution">#</a></span></h1>
<p>执行脚本时返回的对象,可以通过他获取执行的引擎、配置、源码等。</p> <p>执行脚本时返回的对象,可以通过他获取执行的引擎、配置等,也可以停止这个执行</p>
<p>要停止这个脚本的执行,使用<code>exectuion.getEngine().forceStop()</code>.</p>
<h2>ScriptExecution.getEngine()<span><a class="mark" href="#engines_scriptexecution_getengine" id="engines_scriptexecution_getengine">#</a></span></h2> <h2>ScriptExecution.getEngine()<span><a class="mark" href="#engines_scriptexecution_getengine" id="engines_scriptexecution_getengine">#</a></span></h2>
<p>返回执行该脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p> <p>返回执行该脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p>
<h2>ScriptExecution.getConfig()<span><a class="mark" href="#engines_scriptexecution_getconfig" id="engines_scriptexecution_getconfig">#</a></span></h2> <h2>ScriptExecution.getConfig()<span><a class="mark" href="#engines_scriptexecution_getconfig" id="engines_scriptexecution_getconfig">#</a></span></h2>
<p>返回该脚本的运行配置(<a href="#engines_scriptconfig">ScriptConfig</a>)</p> <p>返回该脚本的运行配置(<a href="#engines_scriptconfig">ScriptConfig</a>)</p>
<h2>ScriptExecution.getSource()<span><a class="mark" href="#engines_scriptexecution_getsource" id="engines_scriptexecution_getsource">#</a></span></h2>
<p>返回该脚本的源码对象(<a href="#engines_scriptsource">ScriptSource</a>)</p>
<h1>ScriptEngine<span><a class="mark" href="#engines_scriptengine" id="engines_scriptengine">#</a></span></h1> <h1>ScriptEngine<span><a class="mark" href="#engines_scriptengine" id="engines_scriptengine">#</a></span></h1>
<p>脚本引擎对象。</p>
<h2>ScriptEngine.forceStop()<span><a class="mark" href="#engines_scriptengine_forcestop" id="engines_scriptengine_forcestop">#</a></span></h2> <h2>ScriptEngine.forceStop()<span><a class="mark" href="#engines_scriptengine_forcestop" id="engines_scriptengine_forcestop">#</a></span></h2>
<p>停止脚本引擎的执行。</p> <p>停止脚本引擎的执行。</p>
<h2>ScriptEngine.getTag(tagName)<span><a class="mark" href="#engines_scriptengine_gettag_tagname" id="engines_scriptengine_gettag_tagname">#</a></span></h2> <h2>ScriptEngine.cwd()<span><a class="mark" href="#engines_scriptengine_cwd" id="engines_scriptengine_cwd">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>tagName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 名称</li> <li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>返回对应于tagName的附加在该脚本引擎上的额外信息。tagName包括</p> </div><p>返回脚本执行的路径。对于一个脚本文件而言为这个脚本所在的文件夹;对于其他脚本,例如字符串脚本,则为<code>null</code>或者执行时的设置值。</p>
<ul>
<li><code>source</code> 该脚本引擎当前正在执行的源码(<a href="#engines_scriptsource">ScriptSource</a>)</li>
<li><code>execute_path</code> 该脚本引擎当前执行的路径</li>
</ul>
<p>停止脚本引擎的执行。</p>
<h1>ScriptConfig<span><a class="mark" href="#engines_scriptconfig" id="engines_scriptconfig">#</a></span></h1> <h1>ScriptConfig<span><a class="mark" href="#engines_scriptconfig" id="engines_scriptconfig">#</a></span></h1>
<p>脚本执行时的配置。</p> <p>脚本执行时的配置。</p>
<h2>delay<span><a class="mark" href="#engines_delay" id="engines_delay">#</a></span></h2> <h2>delay<span><a class="mark" href="#engines_delay" id="engines_delay">#</a></span></h2>
<p>延迟执行的毫秒数</p> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>延迟执行的毫秒数</p>
<h2>interval<span><a class="mark" href="#engines_interval" id="engines_interval">#</a></span></h2> <h2>interval<span><a class="mark" href="#engines_interval" id="engines_interval">#</a></span></h2>
<p>循环运行时两次运行之间的时间间隔</p> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>循环运行时两次运行之间的时间间隔</p>
<h2>loopTimes<span><a class="mark" href="#engines_looptimes" id="engines_looptimes">#</a></span></h2> <h2>loopTimes<span><a class="mark" href="#engines_looptimes" id="engines_looptimes">#</a></span></h2>
<p>循环运行次数</p> <div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>循环运行次数</p>
<h2>getPath()<span><a class="mark" href="#engines_getpath" id="engines_getpath">#</a></span></h2> <h2>getPath()<span><a class="mark" href="#engines_getpath" id="engines_getpath">#</a></span></h2>
<p>返回一个字符串数组表示脚本运行时模块寻找的路径。</p> <div class="signature"><ul>
<h1>ScriptSource<span><a class="mark" href="#engines_scriptsource" id="engines_scriptsource">#</a></span></h1> <li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>
<p>脚本执行时的源码对象。可以是字符串源码、文件源码等。</p> </ul>
<p>如果该源码是文件脚本,则可以通过<code>toString()</code>得到该文件的路径。</p> </div><p>返回一个字符串数组表示脚本运行时模块寻找的路径。</p>
<h2>getName()<span><a class="mark" href="#engines_getname" id="engines_getname">#</a></span></h2>
<p>返回该源码的名称。</p>
<h2>getEngineName()<span><a class="mark" href="#engines_getenginename" id="engines_getenginename">#</a></span></h2>
<p>返回执行该源码的脚本引擎的名称。</p>
</div> </div>
</div> </div>

View File

@@ -89,11 +89,31 @@
<li><span class="stability_undefined"><a href="#events_key_down">事件: &#39;key_down&#39;</a></span></li> <li><span class="stability_undefined"><a href="#events_key_down">事件: &#39;key_down&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#events_key_up">事件: &#39;key_up&#39;</a></span></li> <li><span class="stability_undefined"><a href="#events_key_up">事件: &#39;key_up&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#events_exit">事件: &#39;exit`</a></span></li> <li><span class="stability_undefined"><a href="#events_exit">事件: &#39;exit`</a></span></li>
<li><span class="stability_undefined"><a href="#events_obversenotification">obverseNotification()</a></span></li> <li><span class="stability_undefined"><a href="#events_events_observenotification">events.observeNotification()</a></span></li>
<li><span class="stability_undefined"><a href="#events_events_observetoast">events.observeToast()</a></span></li>
<li><span class="stability_undefined"><a href="#events_toast">事件: &#39;toast&#39;</a></span></li> <li><span class="stability_undefined"><a href="#events_toast">事件: &#39;toast&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification">事件: &#39;notification&#39;</a></span></li> <li><span class="stability_undefined"><a href="#events_notification">事件: &#39;notification&#39;</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_undefined"><a href="#events_notification_1">Notification</a></span><ul>
<li><span class="stability_undefined"><a href="#events_notification_number">Notification.number</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_when">Notification.when</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_getpackagename">Notification.getPackageName()</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_gettitle">Notification.getTitle()</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_gettext">Notification.getText()</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_click">Notification.click()</a></span></li>
<li><span class="stability_undefined"><a href="#events_notification_delete">Notification.delete()</a></span></li>
</ul>
</li>
<li><span class="stability_2"><a href="#events_keyevent">KeyEvent</a></span><ul>
<li><span class="stability_undefined"><a href="#events_keyevent_getaction">KeyEvent.getAction()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_getkeycode">KeyEvent.getKeyCode()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_geteventtime">KeyEvent.getEventTime()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_getdowntime">KeyEvent.getDownTime()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_keycodetostring_keycode">KeyEvent.keyCodeToString(keyCode)</a></span></li>
</ul>
</li>
<li><span class="stability_2"><a href="#events_keys">keys</a></span></li>
<li><span class="stability_2"><a href="#events_eventemitter">EventEmitter</a></span><ul> <li><span class="stability_2"><a href="#events_eventemitter">EventEmitter</a></span><ul>
<li><span class="stability_undefined"><a href="#events_eventemitter_defaultmaxlisteners">EventEmitter.defaultMaxListeners</a></span></li> <li><span class="stability_undefined"><a href="#events_eventemitter_defaultmaxlisteners">EventEmitter.defaultMaxListeners</a></span></li>
<li><span class="stability_undefined"><a href="#events_eventemitter_addlistener_eventname_listener">EventEmitter.addListener(eventName, listener)</a></span></li> <li><span class="stability_undefined"><a href="#events_eventemitter_addlistener_eventname_listener">EventEmitter.addListener(eventName, listener)</a></span></li>
@@ -111,15 +131,6 @@
<li><span class="stability_undefined"><a href="#events_eventemitter_setmaxlisteners_n">EventEmitter.setMaxListeners(n)</a></span></li> <li><span class="stability_undefined"><a href="#events_eventemitter_setmaxlisteners_n">EventEmitter.setMaxListeners(n)</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_2"><a href="#events_keyevent">KeyEvent</a></span><ul>
<li><span class="stability_undefined"><a href="#events_keyevent_getaction">KeyEvent.getAction()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_getkeycode">KeyEvent.getKeyCode()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_geteventtime">KeyEvent.getEventTime()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_getdowntime">KeyEvent.getDownTime()</a></span></li>
<li><span class="stability_undefined"><a href="#events_keyevent_keycodetostring_keycode">KeyEvent.keyCodeToString(keyCode)</a></span></li>
</ul>
</li>
<li><span class="stability_2"><a href="#events_keys">Keys</a></span></li>
</ul> </ul>
</div> </div>
@@ -128,18 +139,28 @@
<h1>Events<span><a class="mark" href="#events_events" id="events_events">#</a></span></h1> <h1>Events<span><a class="mark" href="#events_events" id="events_events">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>events模块提供了监听手机通知、按键、触摸的接口。您可以用他配合自动操作函数完成自动化工作。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>events模块提供了监听手机通知、按键、触摸的接口。您可以用他配合自动操作函数完成自动化工作。</p>
<p>events本身是一个<a href="#events_eventemitter">EventEmiiter</a>, 但内置了一些事件、包括按键事件、通知事件、Toast事件等。</p> <p>events本身是一个<a href="#events_eventemitter">EventEmiiter</a>, 但内置了一些事件、包括按键事件、通知事件、Toast事件等。</p>
<h2>events.emitter()<span><a class="mark" href="#events_events_emitter" id="events_events_emitter">#</a></span></h2> <p>需要注意的是,事件的处理是单线程的,并且仍然在原线程执行,如果脚本主体或者其他事件处理中有耗时操作、轮询等,则事件将无法得到及时处理(会进入事件队列等待脚本主体或其他事件处理完成才执行)。例如:</p>
<p>返回一个新的[EventEmitter][]。这个EventEmitter没有内置任何事件。</p> <pre><code>auto();
events.observeNotification();
events.on(&#39;toast&#39;, function(t){
//这段代码将得不到执行
log(t);
});
while(true){
//死循环
}
</code></pre><h2>events.emitter()<span><a class="mark" href="#events_events_emitter" id="events_events_emitter">#</a></span></h2>
<p>返回一个新的<a href="#events_eventemitter">EventEmitter</a>。这个EventEmitter没有内置任何事件。</p>
<h2>events.observeKey()<span><a class="mark" href="#events_events_observekey" id="events_events_observekey">#</a></span></h2> <h2>events.observeKey()<span><a class="mark" href="#events_events_observekey" id="events_events_observekey">#</a></span></h2>
<p>启用按键监听例如音量键、Home键。此函数使用无障碍服务实现,因此此函数会调用auto()确保无障碍服务启用。</p> <p>启用按键监听例如音量键、Home键。按键监听使用无障碍服务实现,如果无障碍服务启用会抛出异常并提示开启</p>
<p>只有这个函数成功执行后, [onKeyDown][], [onKeyUp][]等按键事件的监听才有效。</p> <p>只有这个函数成功执行后, <code>onKeyDown</code>, <code>onKeyUp</code>等按键事件的监听才有效。</p>
<p>该函数在安卓4.3以上才能使用。</p> <p>该函数在安卓4.3以上才能使用。</p>
<h2>events.onKeyDown(keyName, listener)<span><a class="mark" href="#events_events_onkeydown_keyname_listener" id="events_events_onkeydown_keyname_listener">#</a></span></h2> <h2>events.onKeyDown(keyName, listener)<span><a class="mark" href="#events_events_onkeydown_keyname_listener" id="events_events_onkeydown_keyname_listener">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>keyName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要监听的按键名称</li> <li><code>keyName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要监听的按键名称</li>
<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 按键监听器。参数为一个<a href="#events_keyevent">KeyEvent</a></li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 按键监听器。参数为一个<a href="#events_keyevent">KeyEvent</a></li>
</ul> </ul>
</div><p>注册一个按键监听函数当有keyName对应的按键被按下会调用该函数。可用的按键名称参见[Keys][]</p> </div><p>注册一个按键监听函数当有keyName对应的按键被按下会调用该函数。可用的按键名称参见<a href="#events_keys">Keys</a></p>
<p>例如:</p> <p>例如:</p>
<pre><code>//启用按键监听 <pre><code>//启用按键监听
events.observeKey(); events.observeKey();
@@ -198,7 +219,7 @@ events.onKeyDown(&quot;home&quot;, function(event){
<h2>events.observeTouch()<span><a class="mark" href="#events_events_observetouch" id="events_events_observetouch">#</a></span></h2> <h2>events.observeTouch()<span><a class="mark" href="#events_events_observetouch" id="events_events_observetouch">#</a></span></h2>
<p>启用屏幕触摸监听。需要root权限</p> <p>启用屏幕触摸监听。需要root权限</p>
<p>只有这个函数被成功执行后, 触摸事件的监听才有效。</p> <p>只有这个函数被成功执行后, 触摸事件的监听才有效。</p>
<p>没有root权限调用该函数则什么也不会发生。(<strong>注意</strong>: 这个行为未来可能会更改为抛出异常)</p> <p>没有root权限调用该函数则什么也不会发生。</p>
<h2>events.setTouchEventTimeout(timeout)<span><a class="mark" href="#events_events_settoucheventtimeout_timeout" id="events_events_settoucheventtimeout_timeout">#</a></span></h2> <h2>events.setTouchEventTimeout(timeout)<span><a class="mark" href="#events_events_settoucheventtimeout_timeout" id="events_events_settoucheventtimeout_timeout">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 两个触摸事件的最小间隔。单位毫秒。默认为10毫秒。如果number小于0视为0处理。</li> <li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 两个触摸事件的最小间隔。单位毫秒。默认为10毫秒。如果number小于0视为0处理。</li>
@@ -210,7 +231,7 @@ events.onKeyDown(&quot;home&quot;, function(event){
<p>返回触摸事件的最小时间间隔。</p> <p>返回触摸事件的最小时间间隔。</p>
<h2>events.onTouch(listener)<span><a class="mark" href="#events_events_ontouch_listener" id="events_events_ontouch_listener">#</a></span></h2> <h2>events.onTouch(listener)<span><a class="mark" href="#events_events_ontouch_listener" id="events_events_ontouch_listener">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 参数为[Point][]的函数</li> <li><code>listener</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 参数为<a href="images.html#images_point">Point</a>的函数</li>
</ul> </ul>
</div><p>注册一个触摸监听函数。相当于<code>on(&quot;touch&quot;, listener)</code></p> </div><p>注册一个触摸监听函数。相当于<code>on(&quot;touch&quot;, listener)</code></p>
<p>例如:</p> <p>例如:</p>
@@ -237,13 +258,21 @@ events.on(&quot;key&quot;, function(keyCode, event){
}); });
</code></pre><p>其中监听器的参数KeyCode包括</p> </code></pre><p>其中监听器的参数KeyCode包括</p>
<ul> <ul>
<li><code>KeyEvent.KEYCODE_HOME</code> 主页键</li> <li><code>keys.home</code> 主页键</li>
<li><code>KeyEvent.KEYCODE_BACK</code> 返回键</li> <li><code>keys.back</code> 返回键</li>
<li><code>KeyEvent.KEYCODE_MENU</code> 菜单键</li> <li><code>keys.menu</code> 菜单键</li>
<li><code>KeyEvent.KEYCODE_VOLUMEUP</code> 音量上键</li> <li><code>keys.volume_up</code> 音量上键</li>
<li><code>KeyEvent.KEYCODE_VOLUMEDOWN</code> 音量下键</li> <li><code>keys.volume_down</code> 音量下键</li>
</ul> </ul>
<h2>事件: &#39;key_down&#39;<span><a class="mark" href="#events_key_down" id="events_key_down">#</a></span></h2> <p>例如:</p>
<pre><code>auto();
events.observeKey();
events.on(&quot;key&quot;, function(keyCode, event){
if(keyCode == keys.menu &amp;&amp; event.getAction() == event.ACTION_UP){
toast(&quot;菜单键按下&quot;);
}
});
</code></pre><h2>事件: &#39;key_down&#39;<span><a class="mark" href="#events_key_down" id="events_key_down">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>keyCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 键值</li> <li><code>keyCode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 键值</li>
<li><code>event</code> <span class="type">&lt;KeyEvent&gt;</span> 事件</li> <li><code>event</code> <span class="type">&lt;KeyEvent&gt;</span> 事件</li>
@@ -274,18 +303,18 @@ events.on(&quot;exit&quot;, function(){
log(&quot;结束运行&quot;); log(&quot;结束运行&quot;);
}); });
log(&quot;即将结束运行&quot;); log(&quot;即将结束运行&quot;);
</code></pre><h2>obverseNotification()<span><a class="mark" href="#events_obversenotification" id="events_obversenotification">#</a></span></h2> </code></pre><h2>events.observeNotification()<span><a class="mark" href="#events_events_observenotification" id="events_events_observenotification">#</a></span></h2>
<p>开启通知(包括Toast)监听。</p> <p>开启通知监听。例如QQ消息、微信消息、推送等通知</p>
<p>通知与Toast监听依赖于无障碍服务因此这个函数会调用<code>auto()</code>来确保无障碍服务启用。</p> <p>通知监听依赖于通知服务,如果通知服务没有运行,会抛出异常并跳转到通知权限开启界面。(有时即使通知权限已经开启通知服务也没有运行,这时需要关闭权限再重新开启一次)</p>
<p>例如:</p> <p>例如:</p>
<pre><code>events.obverseNotification(); <pre><code>events.obverseNotification();
events.onNotification(function(notification){ events.onNotification(function(notification){
log(notification.getText()); log(notification.getText());
}); });
events.onToast(function(toast){ </code></pre><h2>events.observeToast()<span><a class="mark" href="#events_events_observetoast" id="events_events_observetoast">#</a></span></h2>
log(toast.getText()); <p>开启Toast监听。</p>
}); <p>Toast监听依赖于无障碍服务因此此函数会确保无障碍服务运行。</p>
</code></pre><h2>事件: &#39;toast&#39;<span><a class="mark" href="#events_toast" id="events_toast">#</a></span></h2> <h2>事件: &#39;toast&#39;<span><a class="mark" href="#events_toast" id="events_toast">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>toast</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a><ul> <li><code>toast</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a><ul>
<li><code>getText()</code> 获取Toast的文本内容</li> <li><code>getText()</code> 获取Toast的文本内容</li>
@@ -293,23 +322,91 @@ events.onToast(function(toast){
</ul> </ul>
</li> </li>
</ul> </ul>
</div><p>当有应用发出toast(气泡消息)时会触发该事件。但Auto.js软件本身的toast除外。 </div><p>当有应用发出toast(气泡消息)时会触发该事件。但Auto.js软件本身的toast除外。</p>
例如要记录发出所有toast的应用</p> <p>例如要记录发出所有toast的应用</p>
<pre><code>events.obverseNotification(); <pre><code>events.observeToast();
events.onToast(function(toast){ events.onToast(function(toast){
log(&quot;Toast内容: &quot; + toast.getText() + &quot; 包名: &quot; + toast.getPackageName()); log(&quot;Toast内容: &quot; + toast.getText() + &quot; 包名: &quot; + toast.getPackageName());
}); });
</code></pre><h2>事件: &#39;notification&#39;<span><a class="mark" href="#events_notification" id="events_notification">#</a></span></h2> </code></pre><h2>事件: &#39;notification&#39;<span><a class="mark" href="#events_notification" id="events_notification">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>notification</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 通知</li> <li><code>notification</code> <a href="#events_notification">Notification</a> 通知对象</li>
</ul> </ul>
</div><p>当有应用发出通知时会触发该事件。</p> </div><p>当有应用发出通知时会触发该事件,参数为<a href="#events_notification">Notification</a></p>
<p>例如:</p> <p>例如:</p>
<pre><code>events.observeNotification(); <pre><code>events.observeNotification();
events.on(&quot;notification&quot;, function(notification){ events.on(&quot;notification&quot;, function(n){
log(notification); log(&quot;收到新通知:\n 标题: %s, 内容: %s, \n包名: %s&quot;, n.getTitle(), n.getText(), n.getPackageName());
}); });
</code></pre><p><strong>注意</strong>: 这是一个实验性功能。实测只有某些情况下的通知才能被正确捕捉。</p> </code></pre><h1>Notification<span><a class="mark" href="#events_notification_1" id="events_notification_1">#</a></span></h1>
<p>通知对象,可以获取通知详情,包括通知标题、内容、发出通知的包名、时间等,也可以对通知进行操作,比如点击、删除。</p>
<h2>Notification.number<span><a class="mark" href="#events_notification_number" id="events_notification_number">#</a></span></h2>
<div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>通知数量。例如QQ连续收到两条消息时number为2。</p>
<h2>Notification.when<span><a class="mark" href="#events_notification_when" id="events_notification_when">#</a></span></h2>
<div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>通知发出时间的时间戳,可以用于构造<code>Date</code>对象。例如:</p>
<pre><code>events.observeNotification();
events.on(&quot;notification&quot;, function(n){
log(&quot;通知时间为}&quot; + new Date(n.when));
});
</code></pre><h2>Notification.getPackageName()<span><a class="mark" href="#events_notification_getpackagename" id="events_notification_getpackagename">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>获取发出通知的应用包名。</p>
<h2>Notification.getTitle()<span><a class="mark" href="#events_notification_gettitle" id="events_notification_gettitle">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>获取通知的标题。</p>
<h2>Notification.getText()<span><a class="mark" href="#events_notification_gettext" id="events_notification_gettext">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>获取通知的内容。</p>
<h2>Notification.click()<span><a class="mark" href="#events_notification_click" id="events_notification_click">#</a></span></h2>
<p>点击该通知。例如对于一条QQ消息点击会进入具体的聊天界面。</p>
<h2>Notification.delete()<span><a class="mark" href="#events_notification_delete" id="events_notification_delete">#</a></span></h2>
<p>删除该通知。该通知将从通知栏中消失。</p>
<h1>KeyEvent<span><a class="mark" href="#events_keyevent" id="events_keyevent">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><h2>KeyEvent.getAction()<span><a class="mark" href="#events_keyevent_getaction" id="events_keyevent_getaction">#</a></span></h2>
<p>返回事件的动作。包括:</p>
<ul>
<li><code>KeyEvent.ACTION_DOWN</code> 按下事件</li>
<li><code>KeyEvent.ACTION_UP</code> 弹起事件</li>
</ul>
<h2>KeyEvent.getKeyCode()<span><a class="mark" href="#events_keyevent_getkeycode" id="events_keyevent_getkeycode">#</a></span></h2>
<p>返回按键的键值。包括:</p>
<ul>
<li><code>KeyEvent.KEYCODE_HOME</code> 主页键</li>
<li><code>KeyEvent.KEYCODE_BACK</code> 返回键</li>
<li><code>KeyEvent.KEYCODE_MENU</code> 菜单键</li>
<li><code>KeyEvent.KEYCODE_VOLUME_UP</code> 音量上键</li>
<li><code>KeyEvent.KEYCODE_VOLUME_DOWN</code> 音量下键</li>
</ul>
<h2>KeyEvent.getEventTime()<span><a class="mark" href="#events_keyevent_geteventtime" id="events_keyevent_geteventtime">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>返回事件发生的时间戳。</p>
<h2>KeyEvent.getDownTime()<span><a class="mark" href="#events_keyevent_getdowntime" id="events_keyevent_getdowntime">#</a></span></h2>
<p>返回最近一次按下事件的时间戳。如果本身是按下事件,则与<code>getEventTime()</code>相同。</p>
<h2>KeyEvent.keyCodeToString(keyCode)<span><a class="mark" href="#events_keyevent_keycodetostring_keycode" id="events_keyevent_keycodetostring_keycode">#</a></span></h2>
<p>把键值转换为字符串。例如KEYCODE_HOME转换为&quot;KEYCODE_HOME&quot;</p>
<h1>keys<span><a class="mark" href="#events_keys" id="events_keys">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>按键事件中所有可用的按键名称为:</p>
<ul>
<li><code>volume_up</code> 音量上键</li>
<li><code>volume_down</code> 音量下键</li>
<li><code>home</code> 主屏幕键</li>
<li><code>back</code> 返回键</li>
<li><code>menu</code> 菜单键</li>
</ul>
<h1>EventEmitter<span><a class="mark" href="#events_eventemitter" id="events_eventemitter">#</a></span></h1> <h1>EventEmitter<span><a class="mark" href="#events_eventemitter" id="events_eventemitter">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><h2>EventEmitter.defaultMaxListeners<span><a class="mark" href="#events_eventemitter_defaultmaxlisteners" id="events_eventemitter_defaultmaxlisteners">#</a></span></h2> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><h2>EventEmitter.defaultMaxListeners<span><a class="mark" href="#events_eventemitter_defaultmaxlisteners" id="events_eventemitter_defaultmaxlisteners">#</a></span></h2>
<p>每个事件默认可以注册最多 10 个监听器。 单个 EventEmitter 实例的限制可以使用 emitter.setMaxListeners(n) 方法改变。 所有 EventEmitter 实例的默认值可以使用 EventEmitter.defaultMaxListeners 属性改变。 </p> <p>每个事件默认可以注册最多 10 个监听器。 单个 EventEmitter 实例的限制可以使用 emitter.setMaxListeners(n) 方法改变。 所有 EventEmitter 实例的默认值可以使用 EventEmitter.defaultMaxListeners 属性改变。 </p>
@@ -473,37 +570,6 @@ myEmitter.emit(&#39;event&#39;);
</ul> </ul>
</div><p>默认情况下,如果为特定事件添加了超过 10 个监听器,则 EventEmitter 会打印一个警告。 此限制有助于寻找内存泄露。 但是,并不是所有的事件都要被限为 10 个。 emitter.setMaxListeners() 方法允许修改指定的 EventEmitter 实例的限制。 值设为 Infinity或 0表明不限制监听器的数量。</p> </div><p>默认情况下,如果为特定事件添加了超过 10 个监听器,则 EventEmitter 会打印一个警告。 此限制有助于寻找内存泄露。 但是,并不是所有的事件都要被限为 10 个。 emitter.setMaxListeners() 方法允许修改指定的 EventEmitter 实例的限制。 值设为 Infinity或 0表明不限制监听器的数量。</p>
<p>返回一个 EventEmitter 引用,可以链式调用。</p> <p>返回一个 EventEmitter 引用,可以链式调用。</p>
<h1>KeyEvent<span><a class="mark" href="#events_keyevent" id="events_keyevent">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><h2>KeyEvent.getAction()<span><a class="mark" href="#events_keyevent_getaction" id="events_keyevent_getaction">#</a></span></h2>
<p>返回事件的动作。包括:</p>
<ul>
<li><code>KeyEvent.ACTION_DOWN</code> 按下事件</li>
<li><code>KeyEvent.ACTION_UP</code> 弹起事件</li>
</ul>
<h2>KeyEvent.getKeyCode()<span><a class="mark" href="#events_keyevent_getkeycode" id="events_keyevent_getkeycode">#</a></span></h2>
<p>返回按键的键值。包括:</p>
<ul>
<li><code>KeyEvent.KEYCODE_HOME</code> 主页键</li>
<li><code>KeyEvent.KEYCODE_BACK</code> 返回键</li>
<li><code>KeyEvent.KEYCODE_MENU</code> 菜单键</li>
<li><code>KeyEvent.KEYCODE_VOLUME_UP</code> 音量上键</li>
<li><code>KeyEvent.KEYCODE_VOLUME_DOWN</code> 音量下键</li>
</ul>
<h2>KeyEvent.getEventTime()<span><a class="mark" href="#events_keyevent_geteventtime" id="events_keyevent_geteventtime">#</a></span></h2>
<p>返回事件发生的时间戳。返回值的类型是number。</p>
<h2>KeyEvent.getDownTime()<span><a class="mark" href="#events_keyevent_getdowntime" id="events_keyevent_getdowntime">#</a></span></h2>
<p>返回最近一次按下事件的时间戳。如果本身是按下事件则与getEventTime()相同。</p>
<h2>KeyEvent.keyCodeToString(keyCode)<span><a class="mark" href="#events_keyevent_keycodetostring_keycode" id="events_keyevent_keycodetostring_keycode">#</a></span></h2>
<p>把键值转换为字符串。例如KEYCODE_HOME转换为&quot;KEYCODE_HOME&quot;</p>
<h1>Keys<span><a class="mark" href="#events_keys" id="events_keys">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>按键事件中所有可用的按键名称为:</p>
<ul>
<li><code>volume_up</code> 音量上键</li>
<li><code>volume_down</code> 音量下键</li>
<li><code>home</code> 主屏幕键</li>
<li><code>back</code> 返回键</li>
<li><code>menu</code> 菜单键</li>
</ul>
</div> </div>
</div> </div>

View File

@@ -77,7 +77,7 @@
<li><span class="stability_undefined"><a href="#files_files_isemptydir_path">files.isEmptyDir(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_isemptydir_path">files.isEmptyDir(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_join_parent_child">files.join(parent, child)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_join_parent_child">files.join(parent, child)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_create_path">files.create(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_create_path">files.create(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_createifnotexists_path">files.createIfNotExists(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_createwithdirs_path">files.createWithDirs(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_exists_path">files.exists(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_exists_path">files.exists(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_ensuredir_path">files.ensureDir(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_ensuredir_path">files.ensureDir(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_read_path_encoding_utf_8">files.read(path[, encoding = &quot;utf-8&quot;])</a></span></li> <li><span class="stability_undefined"><a href="#files_files_read_path_encoding_utf_8">files.read(path[, encoding = &quot;utf-8&quot;])</a></span></li>
@@ -96,6 +96,7 @@
<li><span class="stability_undefined"><a href="#files_files_remove_path">files.remove(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_remove_path">files.remove(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_removedir_path">files.removeDir(path)</a></span></li> <li><span class="stability_undefined"><a href="#files_files_removedir_path">files.removeDir(path)</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_getsdcardpath">files.getSdcardPath()</a></span></li> <li><span class="stability_undefined"><a href="#files_files_getsdcardpath">files.getSdcardPath()</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_cwd">files.cwd()</a></span></li>
<li><span class="stability_undefined"><a href="#files_files_listdir_path_filter">files.listDir(path[, filter])</a></span></li> <li><span class="stability_undefined"><a href="#files_files_listdir_path_filter">files.listDir(path[, filter])</a></span></li>
<li><span class="stability_undefined"><a href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192">open(path[, mode = &quot;r&quot;, encoding = &quot;utf-8&quot;, bufferSize = 8192])</a></span></li> <li><span class="stability_undefined"><a href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192">open(path[, mode = &quot;r&quot;, encoding = &quot;utf-8&quot;, bufferSize = 8192])</a></span></li>
</ul> </ul>
@@ -123,68 +124,97 @@
<div id="apicontent"> <div id="apicontent">
<h1>Files<span><a class="mark" href="#files_files" id="files_files">#</a></span></h1> <h1>Files<span><a class="mark" href="#files_files" id="files_files">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>files模块提供了一些常见的文件处理包括文件读写、移动、复制、删掉等。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>files模块提供了一些常见的文件处理包括文件读写、移动、复制、删掉等。</p>
<p>一次性的文件读写可以直接使用<code>files.read()</code>, <code>files.write()</code>, <code>files.append()</code>等方便的函数,但如果需要频繁读写或随机读写,则使用<code>open()</code>函数打开一个文件对象来操作文件,并在操作完毕后调用<code>close()</code>函数关闭文件。</p>
<h2>files.isFile(path)<span><a class="mark" href="#files_files_isfile_path" id="files_files_isfile_path">#</a></span></h2> <h2>files.isFile(path)<span><a class="mark" href="#files_files_isfile_path" id="files_files_isfile_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>返回路径path是否是文件。</p> </div><p>返回路径path是否是文件。</p>
<h2>files.isDir(path)<span><a class="mark" href="#files_files_isdir_path" id="files_files_isdir_path">#</a></span></h2> <pre><code>log(files.isDir(&quot;/sdcard/文件夹/&quot;)); //返回false
log(files.isDir(&quot;/sdcard/文件.txt&quot;)); //返回true
</code></pre><h2>files.isDir(path)<span><a class="mark" href="#files_files_isdir_path" id="files_files_isdir_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>返回路径path是否是文件夹。</p> </div><p>返回路径path是否是文件夹。</p>
<h2>files.isEmptyDir(path)<span><a class="mark" href="#files_files_isemptydir_path" id="files_files_isemptydir_path">#</a></span></h2> <pre><code>log(files.isDir(&quot;/sdcard/文件夹/&quot;)); //返回true
log(files.isDir(&quot;/sdcard/文件.txt&quot;)); //返回false
</code></pre><h2>files.isEmptyDir(path)<span><a class="mark" href="#files_files_isemptydir_path" id="files_files_isemptydir_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>返回文件夹path是否为空文件夹。如果该路径并非文件夹则直接返回false</p> </div><p>返回文件夹path是否为空文件夹。如果该路径并非文件夹则直接返回<code>false</code></p>
<h2>files.join(parent, child)<span><a class="mark" href="#files_files_join_parent_child" id="files_files_join_parent_child">#</a></span></h2> <h2>files.join(parent, child)<span><a class="mark" href="#files_files_join_parent_child" id="files_files_join_parent_child">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 父目录路径</li> <li><code>parent</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 父目录路径</li>
<li><code>child</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 子路径</li> <li><code>child</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 子路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>连接两个路径并返回,例如<code>files.join(&quot;/sdcard/&quot;, &quot;1.txt&quot;)</code>返回&quot;/sdcard/1.txt&quot;</p> </div><p>连接两个路径并返回,例如<code>files.join(&quot;/sdcard/&quot;, &quot;1.txt&quot;)</code>返回&quot;/sdcard/1.txt&quot;</p>
<h2>files.create(path)<span><a class="mark" href="#files_files_create_path" id="files_files_create_path">#</a></span></h2> <h2>files.create(path)<span><a class="mark" href="#files_files_create_path" id="files_files_create_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>创建一个文件并返回是否创建成功。</p> </div><p>创建一个文件或文件夹并返回是否创建成功。如果文件已经存在,则直接返回<code>false</code></p>
<h2>files.createIfNotExists(path)<span><a class="mark" href="#files_files_createifnotexists_path" id="files_files_createifnotexists_path">#</a></span></h2> <pre><code>files.create(&quot;/sdcard/新文件夹/&quot;);
</code></pre><h2>files.createWithDirs(path)<span><a class="mark" href="#files_files_createwithdirs_path" id="files_files_createwithdirs_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>创建一个文件并返回是否创建成功。</p> </div><p>创建一个文件或文件夹并返回是否创建成功。如果文件所在文件夹不存在,则先创建他所在的一系列文件夹。如果文件已经存在,则直接返回<code>false</code></p>
<h2>files.exists(path)<span><a class="mark" href="#files_files_exists_path" id="files_files_exists_path">#</a></span></h2> <pre><code>files.createWithDirs(&quot;/sdcard/新文件夹/新文件夹/新文件夹/1.txt&quot;);
</code></pre><h2>files.exists(path)<span><a class="mark" href="#files_files_exists_path" id="files_files_exists_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>返回在路径path处的文件是否存在。</p> </div><p>返回在路径path处的文件是否存在。</p>
<h2>files.ensureDir(path)<span><a class="mark" href="#files_files_ensuredir_path" id="files_files_ensuredir_path">#</a></span></h2> <h2>files.ensureDir(path)<span><a class="mark" href="#files_files_ensuredir_path" id="files_files_ensuredir_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
</ul> </ul>
</div><p>确保路径path所在的文件夹存在。</p> </div><p>确保路径path所在的文件夹存在。如果该路径所在文件夹不存在,则创建该文件夹。</p>
<p>例如对于路径&quot;/sdcard/Download/ABC/1.txt&quot;,如果/Download/文件夹不存在则会先创建Download再创建ABC文件夹。</p> <p>例如对于路径&quot;/sdcard/Download/ABC/1.txt&quot;,如果/Download/文件夹不存在则会先创建Download再创建ABC文件夹。</p>
<h2>files.read(path[, encoding = &quot;utf-8&quot;])<span><a class="mark" href="#files_files_read_path_encoding_utf_8" id="files_files_read_path_encoding_utf_8">#</a></span></h2> <h2>files.read(path[, encoding = &quot;utf-8&quot;])<span><a class="mark" href="#files_files_read_path_encoding_utf_8" id="files_files_read_path_encoding_utf_8">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码可选默认为utf-8</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>读取文本文件path的所有内容并返回一个字符串</p> </div><p>读取文本文件path的所有内容并返回。如果文件不存在,则抛出<code>FileNotFoundException</code></p>
<h2>files.readBytes(path)<span><a class="mark" href="#files_files_readbytes_path" id="files_files_readbytes_path">#</a></span></h2> <pre><code>log(files.read(&quot;/sdcard/1.txt&quot;));
</code></pre><h2>files.readBytes(path)<span><a class="mark" href="#files_files_readbytes_path" id="files_files_readbytes_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <span class="type">&lt;byte[]&gt;</span></li>
</ul> </ul>
</div><p>读取文件path的所有内容并返回一个字节数组。</p> </div><p>读取文件path的所有内容并返回一个字节数组。如果文件不存在,则抛出<code>FileNotFoundException</code></p>
<p>注意该数组是Java的数组不具有JavaScript数组的函数。</p> <p>注意该数组是Java的数组不具有JavaScript数组的forEach, slice等函数。</p>
<h2>files.write(path, text[, encoding = &quot;utf-8&quot;])<span><a class="mark" href="#files_files_write_path_text_encoding_utf_8" id="files_files_write_path_text_encoding_utf_8">#</a></span></h2> <p>一个以16进制形式打印文件的例子如下:</p>
<pre><code>var data = files.readBytes(&quot;/sdcard/1.png&quot;);
var sb = new java.lang.StringBuilder();
for(var i = 0; i &lt; data.length; i++){
sb.append(data[i].toString(16));
}
log(sb.toString());
</code></pre><h2>files.write(path, text[, encoding = &quot;utf-8&quot;])<span><a class="mark" href="#files_files_write_path_text_encoding_utf_8" id="files_files_write_path_text_encoding_utf_8">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要写入的文本内容</li> <li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要写入的文本内容</li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码</li>
</ul> </ul>
</div><p>把text写入到文件path中。如果文件存在则覆盖不存在则创建。</p> </div><p>把text写入到文件path中。如果文件存在则覆盖不存在则创建。</p>
<h2>files.writeBytes(path, bytes)<span><a class="mark" href="#files_files_writebytes_path_bytes" id="files_files_writebytes_path_bytes">#</a></span></h2> <pre><code>var text = &quot;文件内容&quot;;
//写入文件
files.write(&quot;/sdcard/1.txt&quot;, text);
//用其他应用查看文件
app.viewFile(&quot;/sdcard/1.txt&quot;);
</code></pre><h2>files.writeBytes(path, bytes)<span><a class="mark" href="#files_files_writebytes_path_bytes" id="files_files_writebytes_path_bytes">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>bytes</code> <span class="type">&lt;byte[]&gt;</span> 字节数组,要写入的二进制数据</li> <li><code>bytes</code> <span class="type">&lt;byte[]&gt;</span> 字节数组,要写入的二进制数据</li>
@@ -197,7 +227,12 @@
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码</li>
</ul> </ul>
</div><p>把text追加到文件path的末尾。如果文件不存在则创建。</p> </div><p>把text追加到文件path的末尾。如果文件不存在则创建。</p>
<h2>files.appendBytes(path, text[, encoding = &#39;utf-8&#39;])<span><a class="mark" href="#files_files_appendbytes_path_text_encoding_utf_8" id="files_files_appendbytes_path_text_encoding_utf_8">#</a></span></h2> <pre><code>var text = &quot;追加的文件内容&quot;;
files.append(&quot;/sdcard/1.txt&quot;, text);
files.append(&quot;/sdcard/1.txt&quot;, text);
//用其他应用查看文件
app.viewFile(&quot;/sdcard/1.txt&quot;);
</code></pre><h2>files.appendBytes(path, text[, encoding = &#39;utf-8&#39;])<span><a class="mark" href="#files_files_appendbytes_path_text_encoding_utf_8" id="files_files_appendbytes_path_text_encoding_utf_8">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>bytes</code> <span class="type">&lt;byte[]&gt;</span> 字节数组,要写入的二进制数据</li> <li><code>bytes</code> <span class="type">&lt;byte[]&gt;</span> 字节数组,要写入的二进制数据</li>
@@ -207,75 +242,99 @@
<div class="signature"><ul> <div class="signature"><ul>
<li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要复制的原文件路径</li> <li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要复制的原文件路径</li>
<li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 复制到的文件路径</li> <li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 复制到的文件路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>复制文件。例如<code>files.copy(&quot;/sdcard/1.txt&quot;, &quot;/sdcard/Download/1.txt&quot;)</code></p> </div><p>复制文件,返回是否复制成功。例如<code>files.copy(&quot;/sdcard/1.txt&quot;, &quot;/sdcard/Download/1.txt&quot;)</code></p>
<h2>files.move(fromPath, toPath)<span><a class="mark" href="#files_files_move_frompath_topath" id="files_files_move_frompath_topath">#</a></span></h2> <h2>files.move(fromPath, toPath)<span><a class="mark" href="#files_files_move_frompath_topath" id="files_files_move_frompath_topath">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要移动的原文件路径</li> <li><code>fromPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要移动的原文件路径</li>
<li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 移动到的文件路径</li> <li><code>toPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 移动到的文件路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>移动文件,返回是否移动成功。例如<code>files.move(&quot;/sdcard/1.txt&quot;, &quot;/sdcard/Download/1.txt&quot;)</code>会把1.txt文件从sd卡根目录移动到Download文件夹。</p> </div><p>移动文件,返回是否移动成功。例如<code>files.move(&quot;/sdcard/1.txt&quot;, &quot;/sdcard/Download/1.txt&quot;)</code>会把1.txt文件从sd卡根目录移动到Download文件夹。</p>
<h2>files.rename(path, newName)<span><a class="mark" href="#files_files_rename_path_newname" id="files_files_rename_path_newname">#</a></span></h2> <h2>files.rename(path, newName)<span><a class="mark" href="#files_files_rename_path_newname" id="files_files_rename_path_newname">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的原文件路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的原文件路径</li>
<li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的新文件名</li> <li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的新文件名</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>重命名文件,并返回是否重命名成功。例如<code>files.rename(&quot;/sdcard/1.txt&quot;, &quot;2.txt&quot;)</code></p> </div><p>重命名文件,并返回是否重命名成功。例如<code>files.rename(&quot;/sdcard/1.txt&quot;, &quot;2.txt&quot;)</code></p>
<h2>files.renameWithoutExtension(path, newName)<span><a class="mark" href="#files_files_renamewithoutextension_path_newname" id="files_files_renamewithoutextension_path_newname">#</a></span></h2> <h2>files.renameWithoutExtension(path, newName)<span><a class="mark" href="#files_files_renamewithoutextension_path_newname" id="files_files_renamewithoutextension_path_newname">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的原文件路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的原文件路径</li>
<li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的新文件名</li> <li><code>newName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要重命名的新文件名</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>重命名文件,不包含拓展名,并返回是否重命名成功。例如<code>files.rename(&quot;/sdcard/1.txt&quot;, &quot;2&quot;)</code>会把1.txt重命名为2.txt</p> </div><p>重命名文件,不包含拓展名,并返回是否重命名成功。例如<code>files.rename(&quot;/sdcard/1.txt&quot;, &quot;2&quot;)</code>会把&quot;1.txt&quot;重命名为&quot;2.txt&quot;</p>
<h2>files.getName(path)<span><a class="mark" href="#files_files_getname_path" id="files_files_getname_path">#</a></span></h2> <h2>files.getName(path)<span><a class="mark" href="#files_files_getname_path" id="files_files_getname_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>返回文件的文件名。例如<code>files.getName(&quot;/sdcard/1.txt&quot;)</code>返回&quot;1.txt&quot;</p> </div><p>返回文件的文件名。例如<code>files.getName(&quot;/sdcard/1.txt&quot;)</code>返回&quot;1.txt&quot;</p>
<h2>files.getNameWithoutExtension(path)<span><a class="mark" href="#files_files_getnamewithoutextension_path" id="files_files_getnamewithoutextension_path">#</a></span></h2> <h2>files.getNameWithoutExtension(path)<span><a class="mark" href="#files_files_getnamewithoutextension_path" id="files_files_getnamewithoutextension_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>返回不含拓展名的文件的文件名。例如<code>files.getName(&quot;/sdcard/1.txt&quot;)</code>返回&quot;1&quot;</p> </div><p>返回不含拓展名的文件的文件名。例如<code>files.getName(&quot;/sdcard/1.txt&quot;)</code>返回&quot;1&quot;</p>
<h2>files.getExtension(path)<span><a class="mark" href="#files_files_getextension_path" id="files_files_getextension_path">#</a></span></h2> <h2>files.getExtension(path)<span><a class="mark" href="#files_files_getextension_path" id="files_files_getextension_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>返回文件的拓展名。例如<code>files.getExtension(&quot;/sdcard/1.txt&quot;)</code>返回&quot;txt&quot;</p> </div><p>返回文件的拓展名。例如<code>files.getExtension(&quot;/sdcard/1.txt&quot;)</code>返回&quot;txt&quot;</p>
<h2>files.remove(path)<span><a class="mark" href="#files_files_remove_path" id="files_files_remove_path">#</a></span></h2> <h2>files.remove(path)<span><a class="mark" href="#files_files_remove_path" id="files_files_remove_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>删除文件或<strong>空文件夹</strong>,返回是否删除成功。</p> </div><p>删除文件或<strong>空文件夹</strong>,返回是否删除成功。</p>
<h2>files.removeDir(path)<span><a class="mark" href="#files_files_removedir_path" id="files_files_removedir_path">#</a></span></h2> <h2>files.removeDir(path)<span><a class="mark" href="#files_files_removedir_path" id="files_files_removedir_path">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul> </ul>
</div><p>删除文件夹,如果文件夹不为空,则删除该文件夹的所有内容再删除该文件夹,返回是否全部删除成功。</p> </div><p>删除文件夹,如果文件夹不为空,则删除该文件夹的所有内容再删除该文件夹,返回是否全部删除成功。</p>
<h2>files.getSdcardPath()<span><a class="mark" href="#files_files_getsdcardpath" id="files_files_getsdcardpath">#</a></span></h2> <h2>files.getSdcardPath()<span><a class="mark" href="#files_files_getsdcardpath" id="files_files_getsdcardpath">#</a></span></h2>
<p>返回SD卡路径。所谓SD卡即外部存储器。</p> <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>返回SD卡路径。所谓SD卡即外部存储器。</p>
<h2>files.cwd()<span><a class="mark" href="#files_files_cwd" id="files_files_cwd">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>返回脚本的&quot;当前工作文件夹路径&quot;。该路径指的是,如果脚本本身为脚本文件,则返回这个脚本文件所在目录;否则返回<code>null</code>获取其他设定路径。</p>
<p>例如,对于脚本文件&quot;/sdcard/脚本/1.js&quot;运行<code>files.cwd()</code>返回&quot;/sdcard/脚本/&quot;</p>
<h2>files.listDir(path[, filter])<span><a class="mark" href="#files_files_listdir_path_filter" id="files_files_listdir_path_filter">#</a></span></h2> <h2>files.listDir(path[, filter])<span><a class="mark" href="#files_files_listdir_path_filter" id="files_files_listdir_path_filter">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 路径</li>
<li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 过滤函数,可选。接收一个String参数文件名返回一个Boolean值。</li> <li><code>filter</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 过滤函数,可选。接收一个<code>string</code>参数(文件名),返回一个<code>boolean</code>值。</li>
</ul> </ul>
</div><p>列出文件夹path下的满足条件的文件和文件夹的名称的数组。如果不加filter参数则返回所有文件和文件夹。</p> </div><p>列出文件夹path下的满足条件的文件和文件夹的名称的数组。如果不加filter参数则返回所有文件和文件夹。</p>
<p>例如,获取sdcard目录下的txt文件为</p> <p>列出sdcard目录下所有文件和文件夹为:</p>
<pre><code>var txtFiles = files.listDir(&quot;/sdcard/&quot;, function(name){ <pre><code>var arr = files.listDir(&quot;/sdcard/&quot;);
return name.endsWith(&quot;.txt&quot;) &amp;&amp; files.isFile(&quot;/sdcard/&quot; + name); log(arr);
</code></pre><p>列出脚本目录下所有js脚本文件为:</p>
<pre><code>var dir = &quot;/sdcard/脚本/&quot;;
var jsFiles = files.listDir(dir, function(name){
return name.endsWith(&quot;.js&quot;) &amp;&amp; files.isFile(files.join(dir, name));
}); });
log(jsFiles);
</code></pre><h2>open(path[, mode = &quot;r&quot;, encoding = &quot;utf-8&quot;, bufferSize = 8192])<span><a class="mark" href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192" id="files_open_path_mode_r_encoding_utf_8_buffersize_8192">#</a></span></h2> </code></pre><h2>open(path[, mode = &quot;r&quot;, encoding = &quot;utf-8&quot;, bufferSize = 8192])<span><a class="mark" href="#files_open_path_mode_r_encoding_utf_8_buffersize_8192" id="files_open_path_mode_r_encoding_utf_8_buffersize_8192">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径,例如&quot;/sdcard/1.txt&quot;</li> <li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件路径,例如&quot;/sdcard/1.txt&quot;</li>
<li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件打开模式,包括:<ul> <li><code>mode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文件打开模式,包括:<ul>
<li>&quot;r&quot;: 只读模式。该模式下只能对文件执行<strong>文本</strong>读取操作。</li> <li>&quot;r&quot;: 只读文本模式。该模式下只能对文件执行<strong>文本</strong>读取操作。</li>
<li>&quot;w&quot;: 只写模式。该模式下只能对文件执行<strong>文本</strong>覆盖写入操作。</li> <li>&quot;w&quot;: 只写文本模式。该模式下只能对文件执行<strong>文本</strong>覆盖写入操作。</li>
<li>&quot;a&quot;: 附加模式。该模式下将会把写入的文本附加到文件末尾。<br>目前暂不支持二进制模式,随机读写模式。</li> <li>&quot;a&quot;: 附加文本模式。该模式下将会把写入的文本附加到文件末尾。 </li>
<li>&quot;rw&quot;: 随机读写文本模式。该模式下将会把写入的文本附加到文件末尾。<br>目前暂不支持二进制模式,随机读写模式。</li>
</ul> </ul>
</li> </li>
<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码。</li> <li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 字符编码。</li>
<li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;Number&gt;</a> 文件读写的缓冲区大小。</li> <li><code>bufferSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 文件读写的缓冲区大小。</li>
</ul> </ul>
</div><p>打开一个文件。根据打开模式返回不同的文件对象。包括:</p> </div><p>打开一个文件。根据打开模式返回不同的文件对象。包括:</p>
<ul> <ul>

View File

@@ -72,14 +72,9 @@
<h2>目录</h2> <h2>目录</h2>
<ul> <ul>
<li><span class="stability_undefined"><a href="#globals">全局变量与函数</a></span><ul> <li><span class="stability_undefined"><a href="#globals">全局变量与函数</a></span><ul>
<li><span class="stability_undefined"><a href="#globals_sleep_n">sleep([n])</a></span></li> <li><span class="stability_undefined"><a href="#globals_sleep_n">sleep(n)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_launchpackage_packagename">launchPackage(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_launchapp_appname">launchApp(appName)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_currentpackage">currentPackage()</a></span></li> <li><span class="stability_undefined"><a href="#globals_currentpackage">currentPackage()</a></span></li>
<li><span class="stability_undefined"><a href="#globals_currentactivity">currentActivity()</a></span></li> <li><span class="stability_undefined"><a href="#globals_currentactivity">currentActivity()</a></span></li>
<li><span class="stability_undefined"><a href="#globals_getpackagename_appname">getPackageName(appName)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_getappname_packagename">getAppName(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_openappsetting_packagename">openAppSetting(packageName)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_setclip_text">setClip(text)</a></span></li> <li><span class="stability_undefined"><a href="#globals_setclip_text">setClip(text)</a></span></li>
<li><span class="stability_undefined"><a href="#globals_getclip">getClip()</a></span></li> <li><span class="stability_undefined"><a href="#globals_getclip">getClip()</a></span></li>
<li><span class="stability_undefined"><a href="#globals_toast_message">toast(message)</a></span></li> <li><span class="stability_undefined"><a href="#globals_toast_message">toast(message)</a></span></li>
@@ -98,64 +93,51 @@
<div id="apicontent"> <div id="apicontent">
<h1>全局变量与函数<span><a class="mark" href="#globals" id="globals">#</a></span></h1> <h1>全局变量与函数<span><a class="mark" href="#globals" id="globals">#</a></span></h1>
<p>全局变量和函数在所有模块中均可使用。 但以下变量的作用域只在模块内,详见 module文档</p> <p>全局变量和函数在所有模块中均可使用。 但以下变量的作用域只在模块内,详见 <a href="modules.html">module</a></p>
<ul> <ul>
<li>exports</li> <li>exports</li>
<li>module</li> <li>module</li>
<li>require() <li>require()
以下的对象是特定于 Auto.js 的。 有些内置对象是 JavaScript 语言本身的一部分,它们也是全局的。</li> 以下的对象是特定于 Auto.js 的。 有些内置对象是 JavaScript 语言本身的一部分,它们也是全局的。</li>
</ul> </ul>
<p>一些模块中的函数为了使用方便也可以直接全局使用这些函数在此不再赘述。例如timers模块的setInterval等函数。</p> <p>一些模块中的函数为了使用方便也可以直接全局使用这些函数在此不再赘述。例如timers模块的setInterval, setTimeout等函数。</p>
<h2>sleep([n])<span><a class="mark" href="#globals_sleep_n" id="globals_sleep_n">#</a></span></h2> <h2>sleep(n)<span><a class="mark" href="#globals_sleep_n" id="globals_sleep_n">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 毫秒数</li> <li><code>n</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 毫秒数</li>
</ul> </ul>
</div><p>暂停运行n<strong>毫秒</strong>的时间。1秒等于1000毫秒。</p> </div><p>暂停运行n<strong>毫秒</strong>的时间。1秒等于1000毫秒。</p>
<h2>launchPackage(packageName)<span><a class="mark" href="#globals_launchpackage_packagename" id="globals_launchpackage_packagename">#</a></span></h2> <pre><code>//暂停5毫秒
<div class="signature"><ul> sleep(5000);
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>运行包名为packageName的应用主界面(Launcher)。例如,打开微信为:</p>
<pre><code>launchPackage(&quot;com.tencent.mm&quot;);
</code></pre><p>如果存在多个应用包名相同的情况如双开应用如何处理取决于操作系统。在MIUI中会弹出多开应用的选择界面。</p>
<h2>launchApp(appName)<span><a class="mark" href="#globals_launchapp_appname" id="globals_launchapp_appname">#</a></span></h2>
<div class="signature"><ul>
<li><code>appName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;String&gt;</a> 应用名称</li>
</ul>
</div><p>运行应用名称为appName的应用主界面。当有应用名称相同时只运行其中某一个。<br>例如,打开微信为:</p>
<pre><code>launchApp(&quot;微信&quot;);
</code></pre><h2>currentPackage()<span><a class="mark" href="#globals_currentpackage" id="globals_currentpackage">#</a></span></h2> </code></pre><h2>currentPackage()<span><a class="mark" href="#globals_currentpackage" id="globals_currentpackage">#</a></span></h2>
<p>返回最近一次监测到的正在运行的应用的包名,一般可以认为就是当前正在运行的应用的包名。</p> <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>返回最近一次监测到的正在运行的应用的包名,一般可以认为就是当前正在运行的应用的包名。</p>
<p>此函数依赖于无障碍服务,如果服务未启动,则抛出异常并提示用户启动。</p>
<h2>currentActivity()<span><a class="mark" href="#globals_currentactivity" id="globals_currentactivity">#</a></span></h2> <h2>currentActivity()<span><a class="mark" href="#globals_currentactivity" id="globals_currentactivity">#</a></span></h2>
<p>返回最近一次监测到的正在运行的Activity的名称一般可以认为就是当前正在运行的Activity的名称。</p>
<h2>getPackageName(appName)<span><a class="mark" href="#globals_getpackagename_appname" id="globals_getpackagename_appname">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>appName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用名称</li> <li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul> </ul>
</div><p>获取应用的包名。例如getPackageName(&quot;QQ&quot;)为&quot;com.tencent.mobileqq&quot;。如果有相同名称的应用只返回其中某一个的包名。如果不存在这个名称的应用会返回null</p> </div><p>返回最近一次监测到的正在运行的Activity的名称一般可以认为就是当前正在运行的Activity的名称</p>
<h2>getAppName(packageName)<span><a class="mark" href="#globals_getappname_packagename" id="globals_getappname_packagename">#</a></span></h2> <p>此函数依赖于无障碍服务,如果服务未启动,则抛出异常并提示用户启动。</p>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>返回对应包名的应用的名称。如果应用不存在返回null。</p>
<h2>openAppSetting(packageName)<span><a class="mark" href="#globals_openappsetting_packagename" id="globals_openappsetting_packagename">#</a></span></h2>
<div class="signature"><ul>
<li><code>packageName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 应用包名</li>
</ul>
</div><p>打开某个应用的应用详情页也就是管理应用权限和可以停止其运行的页面。如果应用包名不存在则返回false否则返回true。</p>
<h2>setClip(text)<span><a class="mark" href="#globals_setclip_text" id="globals_setclip_text">#</a></span></h2> <h2>setClip(text)<span><a class="mark" href="#globals_setclip_text" id="globals_setclip_text">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文本</li> <li><code>text</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 文本</li>
</ul> </ul>
</div><p>设置剪贴板内容。此剪贴板即系统剪贴板,在一般应用的输入框中&quot;粘贴&quot;既可使用。</p> </div><p>设置剪贴板内容。此剪贴板即系统剪贴板,在一般应用的输入框中&quot;粘贴&quot;既可使用。</p>
<h2>getClip()<span><a class="mark" href="#globals_getclip" id="globals_getclip">#</a></span></h2> <pre><code>setClip(&quot;剪贴板文本&quot;);
<p>返回系统剪贴板的内容。</p> </code></pre><h2>getClip()<span><a class="mark" href="#globals_getclip" id="globals_getclip">#</a></span></h2>
<h2>toast(message)<span><a class="mark" href="#globals_toast_message" id="globals_toast_message">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>message <span class="type">&lt;string>&gt;</span> | <span class="type">&lt;{Object&gt;</span> 要显示的信息</li> <li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>返回系统剪贴板的内容。</p>
<pre><code>toast(&quot;剪贴板内容为:&quot; + getClip());
</code></pre><h2>toast(message)<span><a class="mark" href="#globals_toast_message" id="globals_toast_message">#</a></span></h2>
<div class="signature"><ul>
<li>message <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要显示的信息</li>
</ul> </ul>
</div><p>以气泡显示信息message几秒。(具体时间取决于安卓系统一般都是2秒)</p> </div><p>以气泡显示信息message几秒。(具体时间取决于安卓系统一般都是2秒)</p>
<p>注意,信息的显示是&quot;异步&quot;执行的(不属于Looper循环),并且,不会等待信息消失程序才继续执行。如果在循环中执行该命令,可能出现脚本停止运行后仍然有不断的气泡信息出现的情况。 <p>注意,信息的显示是&quot;异步&quot;执行的,并且,不会等待信息消失程序才继续执行。如果在循环中执行该命令,可能出现脚本停止运行后仍然有不断的气泡信息出现的情况。
例如:</p> 例如:</p>
<pre><code>for(var i = 0; i &lt; 100; i++){ <pre><code>for(var i = 0; i &lt; 100; i++){
toast(i); toast(i);
@@ -174,11 +156,10 @@ toast = function(message){
} }
for(var i = 0; i &lt; 100; i++){ for(var i = 0; i &lt; 100; i++){
toast(i); toast(i);
sleep(2000);
} }
</code></pre><h2>toastLog(message)<span><a class="mark" href="#globals_toastlog_message" id="globals_toastlog_message">#</a></span></h2> </code></pre><h2>toastLog(message)<span><a class="mark" href="#globals_toastlog_message" id="globals_toastlog_message">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li>message \<String\> | \<Object\> 要显示的信息</li> <li>message <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要显示的信息</li>
</ul> </ul>
</div><p>相当于<code>toast(message);log(message)</code>。显示信息message并在控制台中输出。参见console.log。</p> </div><p>相当于<code>toast(message);log(message)</code>。显示信息message并在控制台中输出。参见console.log。</p>
<h2>waitForActivity(activity[, period = 200])<span><a class="mark" href="#globals_waitforactivity_activity_period_200" id="globals_waitforactivity_activity_period_200">#</a></span></h2> <h2>waitForActivity(activity[, period = 200])<span><a class="mark" href="#globals_waitforactivity_activity_period_200" id="globals_waitforactivity_activity_period_200">#</a></span></h2>
@@ -195,14 +176,19 @@ for(var i = 0; i &lt; 100; i++){
</div><p>等待指定的应用出现。例如<code>waitForPackage(&quot;com.tencent.mm&quot;)</code>为等待当前界面为微信。</p> </div><p>等待指定的应用出现。例如<code>waitForPackage(&quot;com.tencent.mm&quot;)</code>为等待当前界面为微信。</p>
<h2>exit()<span><a class="mark" href="#globals_exit" id="globals_exit">#</a></span></h2> <h2>exit()<span><a class="mark" href="#globals_exit" id="globals_exit">#</a></span></h2>
<p>立即停止脚本运行。</p> <p>立即停止脚本运行。</p>
<p>立即停止是通过抛出<code>ScriptInterrupttedException</code>来实现的,因此如果用<code>try...catch</code>把exit()函数的异常捕捉,则脚本不会立即停止,仍会运行几行后再停止。</p>
<h2>random(min, max)<span><a class="mark" href="#globals_random_min_max" id="globals_random_min_max">#</a></span></h2> <h2>random(min, max)<span><a class="mark" href="#globals_random_min_max" id="globals_random_min_max">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 随机数产生的区间下界</li> <li><code>min</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 随机数产生的区间下界</li>
<li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 随机数产生的区间上界</li> <li><code>max</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 随机数产生的区间上界</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul> </ul>
</div><p>返回一个在[min...max]之间的随机数。例如random(0, 2)可能产生0, 1, 2.</p> </div><p>返回一个在[min...max]之间的随机数。例如random(0, 2)可能产生0, 1, 2</p>
<h2>random()<span><a class="mark" href="#globals_random" id="globals_random">#</a></span></h2> <h2>random()<span><a class="mark" href="#globals_random" id="globals_random">#</a></span></h2>
<p>返回在[0, 1)的随机浮点数。</p> <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>返回在[0, 1)的随机浮点数。</p>
<h2>context<span><a class="mark" href="#globals_context" id="globals_context">#</a></span></h2> <h2>context<span><a class="mark" href="#globals_context" id="globals_context">#</a></span></h2>
<p>全局变量。一个android.content.Context对象。</p> <p>全局变量。一个android.content.Context对象。</p>
<p>注意该对象为ApplicationContext因此不能用于界面、对话框等的创建。</p> <p>注意该对象为ApplicationContext因此不能用于界面、对话框等的创建。</p>

View File

@@ -103,27 +103,49 @@
<div id="apicontent"> <div id="apicontent">
<h1>Keys<span><a class="mark" href="#keys_keys" id="keys_keys">#</a></span></h1> <h1>Keys<span><a class="mark" href="#keys_keys" id="keys_keys">#</a></span></h1>
<p>按键模拟部分提供了一些模拟物理按键的全局函数包括Home、音量键、照相键等有的函数依赖于无障碍服务有的函数依赖于root权限。</p> <p>按键模拟部分提供了一些模拟物理按键的全局函数包括Home、音量键、照相键等有的函数依赖于无障碍服务有的函数依赖于root权限。</p>
<p>一般来说以大写字母开头的函数都依赖于root权限。执行此类函数时如果没有root权限则函数执行后没有效果并会在控制台输出一个警告。</p>
<h2>back()<span><a class="mark" href="#keys_back" id="keys_back">#</a></span></h2> <h2>back()<span><a class="mark" href="#keys_back" id="keys_back">#</a></span></h2>
<p>模拟按下返回键。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>模拟按下返回键。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>home()<span><a class="mark" href="#keys_home" id="keys_home">#</a></span></h2> <h2>home()<span><a class="mark" href="#keys_home" id="keys_home">#</a></span></h2>
<p>模拟按下Home键。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>模拟按下Home键。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>powerDialog()<span><a class="mark" href="#keys_powerdialog" id="keys_powerdialog">#</a></span></h2> <h2>powerDialog()<span><a class="mark" href="#keys_powerdialog" id="keys_powerdialog">#</a></span></h2>
<p>弹出电源键菜单。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>弹出电源键菜单。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>notifications()<span><a class="mark" href="#keys_notifications" id="keys_notifications">#</a></span></h2> <h2>notifications()<span><a class="mark" href="#keys_notifications" id="keys_notifications">#</a></span></h2>
<p>弹出通知栏。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>拉出通知栏。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>quickSettings()<span><a class="mark" href="#keys_quicksettings" id="keys_quicksettings">#</a></span></h2> <h2>quickSettings()<span><a class="mark" href="#keys_quicksettings" id="keys_quicksettings">#</a></span></h2>
<p>显示快速设置(下拉通知栏到底)。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>显示快速设置(下拉通知栏到底)。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>recents()<span><a class="mark" href="#keys_recents" id="keys_recents">#</a></span></h2> <h2>recents()<span><a class="mark" href="#keys_recents" id="keys_recents">#</a></span></h2>
<p>显示最近任务。返回是否执行成功。 <div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>显示最近任务。返回是否执行成功。
此函数依赖于无障碍服务。</p> 此函数依赖于无障碍服务。</p>
<h2>splitScreen()<span><a class="mark" href="#keys_splitscreen" id="keys_splitscreen">#</a></span></h2> <h2>splitScreen()<span><a class="mark" href="#keys_splitscreen" id="keys_splitscreen">#</a></span></h2>
<p>分屏。返回是否执行成功。 <div class="signature"><ul>
此函数依赖于无障碍服务。</p> <li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>分屏。返回是否执行成功。
此函数依赖于无障碍服务, 并且需要系统自身功能的支持。</p>
<h2>Home()<span><a class="mark" href="#keys_home_1" id="keys_home_1">#</a></span></h2> <h2>Home()<span><a class="mark" href="#keys_home_1" id="keys_home_1">#</a></span></h2>
<p>模拟按下Home键。 <p>模拟按下Home键。
此函数依赖于root权限。</p> 此函数依赖于root权限。</p>

View File

@@ -80,7 +80,7 @@
<h1>module (模块)<span><a class="mark" href="#modules_module" id="modules_module">#</a></span></h1> <h1>module (模块)<span><a class="mark" href="#modules_module" id="modules_module">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>Auto.js 有一个简单的模块加载系统。 在 Auto.js 中,文件和模块是一一对应的(每个文件被视为一个独立的模块)。</p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>Auto.js 有一个简单的模块加载系统。 在 Auto.js 中,文件和模块是一一对应的(每个文件被视为一个独立的模块)。</p>
<p>例子,假设有一个名为 foo.js 的文件:</p> <p>例子,假设有一个名为 foo.js 的文件:</p>
<pre><code>const circle = require(&#39;./circle.js&#39;); <pre><code>const circle = require(&#39;circle.js&#39;);
console.log(&quot;半径为 4 的圆的面积是 %d&quot;, circle.area(4)); console.log(&quot;半径为 4 的圆的面积是 %d&quot;, circle.area(4));
</code></pre><p>在第一行中foo.js 加载了同一目录下的 circle.js 模块。</p> </code></pre><p>在第一行中foo.js 加载了同一目录下的 circle.js 模块。</p>
<p>circle.js 文件的内容为:</p> <p>circle.js 文件的内容为:</p>
@@ -94,12 +94,12 @@ circle.circumference = (r) =&gt; 2 * PI * r;
module.exports = circle; module.exports = circle;
</code></pre><p>circle.js 模块导出了 area() 和 circumference() 两个函数。 通过在特殊的 exports 对象上指定额外的属性,函数和对象可以被添加到模块的根部。</p> </code></pre><p>circle.js 模块导出了 area() 和 circumference() 两个函数。 通过在特殊的 exports 对象上指定额外的属性,函数和对象可以被添加到模块的根部。</p>
<p>模块内的本地变量是私有的,因为模块被 Node.js 包装在一个函数中(详见模块包装器)。 在这个例子中,变量 PI 是 circle.js 私有的。</p> <p>模块内的本地变量是私有的。 在这个例子中,变量 PI 是 circle.js 私有的,不会影响到加载他的脚本的变量环境</p>
<p>module.exports属性可以被赋予一个新的值例如函数或对象</p> <p>module.exports属性可以被赋予一个新的值例如函数或对象</p>
<p>如下bar.js 会用到 square 模块square 导出一个构造函数:</p> <p>如下bar.js 会用到 square 模块square 导出一个构造函数:</p>
<pre><code>const square = require(&#39;./square.js&#39;); <pre><code>const square = require(&#39;square.js&#39;);
const mySquare = square(2); const mySquare = square(2);
console.log(`正方形的面积是 ${mySquare.area()}`); console.log(&quot;正方形的面积是 %d&quot;, mySquare.area());
square 模块定义在 square.js 中: square 模块定义在 square.js 中:
// 赋值给 `exports` 不会修改模块,必须使用 `module.exports` // 赋值给 `exports` 不会修改模块,必须使用 `module.exports`

View File

@@ -157,11 +157,11 @@
<li>cmd <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要执行的命令</li> <li>cmd <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要执行的命令</li>
<li>root <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;Boolean&gt;</a> 是否以root权限运行默认为false。</li> <li>root <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;Boolean&gt;</a> 是否以root权限运行默认为false。</li>
</ul> </ul>
</div><p>返回运行一个对象表示命令的执行结果。其属性如下:</p> </div><p>一次性执行命令cmd, 并返回命令的执行结果。返回对象的其属性如下:</p>
<ul> <ul>
<li>code <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 返回码。执行成功时为0失败时为非0的数字。</li> <li>code <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 返回码。执行成功时为0失败时为非0的数字。</li>
<li>result <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 运行结果(stdout输出结果)</li> <li>result <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 运行结果(stdout输出结果)</li>
<li>error \<String\> 运行的错误信息(stderr输出结果)。例如执行需要root权限的命令但没有授予root权限会返回错误信息&quot;Permission denied&quot;</li> <li>error <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 运行的错误信息(stderr输出结果)。例如执行需要root权限的命令但没有授予root权限会返回错误信息&quot;Permission denied&quot;</li>
</ul> </ul>
<p>示例(强制停止微信) </p> <p>示例(强制停止微信) </p>
<pre><code>var result = shell(&quot;am force-stop com.tencent.mm&quot;, true); <pre><code>var result = shell(&quot;am force-stop com.tencent.mm&quot;, true);
@@ -180,6 +180,7 @@ if(result.code == 0){
</ul> </ul>
</div><p>Shell对象的&quot;构造函数&quot;</p> </div><p>Shell对象的&quot;构造函数&quot;</p>
<pre><code>var sh = new Shell(true); <pre><code>var sh = new Shell(true);
//强制停止微信
sh.exec(&quot;am force-stop com.tencent.mm&quot;); sh.exec(&quot;am force-stop com.tencent.mm&quot;);
sh.exit(); sh.exit();
</code></pre><h2>Shell.exec(cmd)<span><a class="mark" href="#shell_shell_exec_cmd" id="shell_shell_exec_cmd">#</a></span></h2> </code></pre><h2>Shell.exec(cmd)<span><a class="mark" href="#shell_shell_exec_cmd" id="shell_shell_exec_cmd">#</a></span></h2>
@@ -190,7 +191,7 @@ sh.exit();
<p>注意,命令执行是&quot;异步&quot;的、非阻塞的。也就是不会等待命令完成后才继续向下执行。</p> <p>注意,命令执行是&quot;异步&quot;的、非阻塞的。也就是不会等待命令完成后才继续向下执行。</p>
<p>尽管这样的设计使用起来有很多不便之处,但受限于终端模拟器,暂时没有解决方式;如果后续能找到解决方案,则将提供<code>Shell.execAndWaitFor</code>函数。</p> <p>尽管这样的设计使用起来有很多不便之处,但受限于终端模拟器,暂时没有解决方式;如果后续能找到解决方案,则将提供<code>Shell.execAndWaitFor</code>函数。</p>
<h2>Shell.exit()<span><a class="mark" href="#shell_shell_exit" id="shell_shell_exit">#</a></span></h2> <h2>Shell.exit()<span><a class="mark" href="#shell_shell_exit" id="shell_shell_exit">#</a></span></h2>
<p>直接退出shell。这意味着正在执行的命令会被强制退出。</p> <p>直接退出shell。正在执行的命令会被强制退出。</p>
<h2>Shell.exitAndWaitFor()<span><a class="mark" href="#shell_shell_exitandwaitfor" id="shell_shell_exitandwaitfor">#</a></span></h2> <h2>Shell.exitAndWaitFor()<span><a class="mark" href="#shell_shell_exitandwaitfor" id="shell_shell_exitandwaitfor">#</a></span></h2>
<p>执行&quot;exit&quot;命令并等待执行命令执行完成、退出shell。</p> <p>执行&quot;exit&quot;命令并等待执行命令执行完成、退出shell。</p>
<p>此函数会执行exit命令来正常退出shell。</p> <p>此函数会执行exit命令来正常退出shell。</p>
@@ -200,21 +201,24 @@ sh.exit();
</ul> </ul>
</div><p>设置该Shell的回调函数以便监听Shell的输出。可以包括以下属性</p> </div><p>设置该Shell的回调函数以便监听Shell的输出。可以包括以下属性</p>
<ul> <ul>
<li>onOutput <span class="type">&lt;function&gt;</span> 每当shell有新的输出时便会调用该函数。其参数是一个字符串。</li> <li>onOutput <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 每当shell有新的输出时便会调用该函数。其参数是一个字符串。</li>
<li>onNewLine <span class="type">&lt;function&gt;</span> 每当shell有新的一行输出时便会调用该函数。其参数是一个字符串(不包括最后的换行符)。</li> <li>onNewLine <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 每当shell有新的一行输出时便会调用该函数。其参数是一个字符串(不包括最后的换行符)。</li>
</ul> </ul>
<p>例如:</p> <p>例如:</p>
<pre><code>var sh = new Shell(); <pre><code>var sh = new Shell();
sh.setCallback({ sh.setCallback({
onNewLine: function(line){ onNewLine: function(line){
//有新的一行输出时打印到控制台
log(line); log(line);
} }
}) })
while(true){ while(true){
//循环输入命令
var cmd = dialogs.rawInput(&quot;请输入要执行的命令输入exit退出&quot;); var cmd = dialogs.rawInput(&quot;请输入要执行的命令输入exit退出&quot;);
if(cmd == &quot;exit&quot;){ if(cmd == &quot;exit&quot;){
break; break;
} }
//执行命令
sh.exec(cmd); sh.exec(cmd);
} }
sh.exit(); sh.exit();
@@ -222,7 +226,7 @@ sh.exit();
<p>以下关于shell命令的资料来自<a href="https://developer.android.com/studio/command-line/adb.html#shellcommands">AndroidStudio用户指南Shell命令</a></p> <p>以下关于shell命令的资料来自<a href="https://developer.android.com/studio/command-line/adb.html#shellcommands">AndroidStudio用户指南Shell命令</a></p>
<h2>am命令<span><a class="mark" href="#shell_am" id="shell_am">#</a></span></h2> <h2>am命令<span><a class="mark" href="#shell_am" id="shell_am">#</a></span></h2>
<p>am命令即Activity Manager命令用于管理应用程序活动、服务等。</p> <p>am命令即Activity Manager命令用于管理应用程序活动、服务等。</p>
<p><strong>以下命令均以&quot;am &quot;开头,例如&quot;shell(\&quot;am start -p com.tencent.mm\&quot;);&quot;(启动微信)</strong></p> <p><strong>以下命令均以&quot;am &quot;开头,例如<code>shell(&#39;am start -p com.tencent.mm&#39;);</code>(启动微信)</strong></p>
<h3>start [options] intent<span><a class="mark" href="#shell_start_options_intent" id="shell_start_options_intent">#</a></span></h3> <h3>start [options] intent<span><a class="mark" href="#shell_start_options_intent" id="shell_start_options_intent">#</a></span></h3>
<p>启动 intent 指定的 Activity(应用程序活动)。<br>请参阅 <a href="#shell_intent">intent 参数的规范</a></p> <p>启动 intent 指定的 Activity(应用程序活动)。<br>请参阅 <a href="#shell_intent">intent 参数的规范</a></p>
<p>选项包括:</p> <p>选项包括:</p>

View File

@@ -105,7 +105,7 @@ storage.put(&quot;a&quot;, 123);
</code></pre><p>而在另一个脚本中是可以获取到ABC以及a的值的</p> </code></pre><p>而在另一个脚本中是可以获取到ABC以及a的值的</p>
<pre><code>var storage = storages.create(&quot;ABC&quot;); <pre><code>var storage = storages.create(&quot;ABC&quot;);
log(&quot;a = &quot; + storage.get(&quot;a&quot;)); log(&quot;a = &quot; + storage.get(&quot;a&quot;));
</code></pre><p>因此,本地存储的名称比较重要,尽量使用含有域名、作者邮箱等信息的名称来避免冲突,例如:</p> </code></pre><p>因此,本地存储的名称比较重要,尽量使用含有域名、作者邮箱等唯一信息的名称来避免冲突,例如:</p>
<pre><code>var storage = storages.create(&quot;2732014414@qq.com:ABC&quot;); <pre><code>var storage = storages.create(&quot;2732014414@qq.com:ABC&quot;);
</code></pre><h2>storages.remove(name)<span><a class="mark" href="#storages_storages_remove_name" id="storages_storages_remove_name">#</a></span></h2> </code></pre><h2>storages.remove(name)<span><a class="mark" href="#storages_storages_remove_name" id="storages_storages_remove_name">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>

View File

@@ -73,31 +73,333 @@
<ul> <ul>
<li><span class="stability_1"><a href="#threads_threads">Threads</a></span><ul> <li><span class="stability_1"><a href="#threads_threads">Threads</a></span><ul>
<li><span class="stability_undefined"><a href="#threads_threads_start_action">threads.start(action)</a></span></li> <li><span class="stability_undefined"><a href="#threads_threads_start_action">threads.start(action)</a></span></li>
<li><span class="stability_undefined"><a href="#threads_threads_shutdownall">threads.shutDownAll()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_threads_currentthread">threads.currentThread()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_threads_disposable">threads.disposable()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_threads_atomic_initialvalue">threads.atomic([initialValue])</a></span></li>
<li><span class="stability_undefined"><a href="#threads_threads_lock">threads.lock()</a></span></li>
</ul> </ul>
</li> </li>
<li><span class="stability_undefined"><a href="#threads_thread">Thread</a></span><ul>
<li><span class="stability_undefined"><a href="#threads_thread_interrupt">Thread.interrupt()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_join_timeout">Thread.join([timeout])</a></span></li>
<li><span class="stability_undefined"><a href="#threads_isalive">isAlive()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_waitfor">waitFor()</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_settimeout_callback_delay_args">Thread.setTimeout(callback, delay[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_setinterval_callback_delay_args">Thread.setInterval(callback, delay[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_setimmediate_callback_args">Thread.setImmediate(callback[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_clearinterval_id">Thread.clearInterval(id)</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_cleartimeout_id">Thread.clearTimeout(id)</a></span></li>
<li><span class="stability_undefined"><a href="#threads_thread_clearimmediate_id">Thread.clearImmediate(id)</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#threads">线程安全</a></span><ul>
<li><span class="stability_undefined"><a href="#threads_sync_func">sync(func)</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#threads_1">线程通信</a></span></li>
</ul> </ul>
</div> </div>
<div id="apicontent"> <div id="apicontent">
<h1>Threads<span><a class="mark" href="#threads_threads" id="threads_threads">#</a></span></h1> <h1>Threads<span><a class="mark" href="#threads_threads" id="threads_threads">#</a></span></h1>
<div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experiment</div><p>threads模块提供了多线程支持可以启动新线程来运行脚本。新线程会在脚本停止时也自动停止。</p> <div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experiment</div><p>threads模块提供了多线程支持可以启动新线程来运行脚本。</p>
<p>但是在新线程中暂时不能使用timers模块的函数包括setTimeout, setInterval等。而且目前在新线程调用exit()函数时只会退出当前线程。</p> <p>脚本主线程会等待所有子线程执行完成后才停止执行,因此如果子线程中有死循环,请在必要的时候调用<code>exit()</code>来直接停止脚本或<code>threads.shutDownAll()</code>来停止所有子线程。</p>
<p>通过<code>threads.start()</code>启动的所有线程会在脚本被强制停止时自动停止。</p>
<p>由于JavaScript自身没有多线程的支持因此您可能会遇到意料之外的问题。</p>
<h2>threads.start(action)<span><a class="mark" href="#threads_threads_start_action" id="threads_threads_start_action">#</a></span></h2> <h2>threads.start(action)<span><a class="mark" href="#threads_threads_start_action" id="threads_threads_start_action">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 要在新线程执行的函数</li> <li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 要在新线程执行的函数</li>
<li>返回 <a href="#threads_thread">Thread</a></li>
</ul> </ul>
</div><p>启动一个新线程并执行action。</p> </div><p>启动一个新线程并执行action。</p>
<p>例如:</p> <p>例如:</p>
<pre><code>threads.start(function(){ <pre><code>threads.start(function(){
//在新线程执行的代码
while(true){ while(true){
log(&quot;线程2&quot;); log(&quot;线程&quot;);
} }
}); });
while(true){ while(true){
log(&quot;线程1&quot;); log(&quot;脚本主线程&quot;);
} }
</code></pre> </code></pre><p>通过该函数返回的<a href="#threads_thread">Thread</a>对象可以获取该线程的状态,控制该线程的运行中。例如:</p>
<pre><code>var thread = threads.start(function(){
while(true){
log(&quot;子线程&quot;);
}
});
//停止线程执行
thread.interrupt();
</code></pre><p>更多信息参见<a href="#threads_thread">Thread</a></p>
<h2>threads.shutDownAll()<span><a class="mark" href="#threads_threads_shutdownall" id="threads_threads_shutdownall">#</a></span></h2>
<p>停止所有通过<code>threads.start()</code>启动的子线程。</p>
<h2>threads.currentThread()<span><a class="mark" href="#threads_threads_currentthread" id="threads_threads_currentthread">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="#threads_thread">Thread</a></li>
</ul>
</div><p>返回当前线程。</p>
<h2>threads.disposable()<span><a class="mark" href="#threads_threads_disposable" id="threads_threads_disposable">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="#threads_disposable">Disposable</a></li>
</ul>
</div><p>新建一个Disposable对象用于等待另一个线程的某个一次性结果。更多信息参见<a href="#threads_线程通信">线程通信</a>以及<a href="#threads_disposable">Disposable</a></p>
<h2>threads.atomic([initialValue])<span><a class="mark" href="#threads_threads_atomic_initialvalue" id="threads_threads_atomic_initialvalue">#</a></span></h2>
<div class="signature"><ul>
<li><code>initialValue</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 初始整数值默认为0</li>
<li>返回<a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html">AtomicLong</a></li>
</ul>
</div><p>新建一个整数原子变量。更多信息参见<a href="#threads_线程安全">线程安全</a>以及<a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html">AtomicLong</a></p>
<h2>threads.lock()<span><a class="mark" href="#threads_threads_lock" id="threads_threads_lock">#</a></span></h2>
<div class="signature"><ul>
<li>返回<a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html">ReentrantLock</a></li>
</ul>
</div><p>新建一个可重入锁。更多信息参见<a href="#threads_线程安全">线程安全</a>以及<a href="https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html">ReentrantLock</a></p>
<h1>Thread<span><a class="mark" href="#threads_thread" id="threads_thread">#</a></span></h1>
<p>线程对象,<code>threads.start()</code>返回的对象,用于获取和控制线程的状态,与其他线程交互等。</p>
<p>Thread对象提供了和timers模块一样的API例如<code>setTimeout()</code>, <code>setInterval()</code>等,用于在该线程执行相应的定时回调,从而使线程之间可以直接交互。例如:</p>
<pre><code>var thread = threads.start(function(){
//在子线程执行的定时器
setInterval(function(){
log(&quot;子线程:&quot; + threads.currentThread());
}, 1000);
});
log(&quot;当前线程为主线程:&quot; + threads.currentThread());
//等待子线程启动
thread.waitFor();
//在子线程执行的定时器
thread.setTimeout(function(){
//这段代码会在子线程执行
log(&quot;当前线程为子线程:&quot; + threads.currentThread());
}, 2000);
sleep(30 * 1000);
thread.interrupt();
</code></pre><h2>Thread.interrupt()<span><a class="mark" href="#threads_thread_interrupt" id="threads_thread_interrupt">#</a></span></h2>
<p>中断线程运行。</p>
<h2>Thread.join([timeout])<span><a class="mark" href="#threads_thread_join_timeout" id="threads_thread_join_timeout">#</a></span></h2>
<div class="signature"><ul>
<li><code>timeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 等待时间,单位毫秒</li>
</ul>
</div><p>等待线程执行完成。如果timeout为0则会一直等待直至该线程执行完成否则最多等待timeout毫秒的时间。</p>
<p>例如:</p>
<pre><code>var sum = 0;
//启动子线程计算1加到10000
var thread = threads.start(function(){
for(var i = 0; i &lt; 10000; i++){
sum += i;
}
});
//等待该线程完成
thread.join();
toast(&quot;sum = &quot; + sum);
</code></pre><h2>isAlive()<span><a class="mark" href="#threads_isalive" id="threads_isalive">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</div><p>返回线程是否存活。如果线程仍未开始或已经结束,返回<code>false</code>; 如果线程已经开始或者正在运行中,返回<code>true</code></p>
<h2>waitFor()<span><a class="mark" href="#threads_waitfor" id="threads_waitfor">#</a></span></h2>
<p>等待线程开始执行。调用<code>threads.start()</code>以后线程仍然需要一定时间才能开始执行,因此调用此函数会等待线程开始执行;如果线程已经处于执行状态则立即返回。</p>
<pre><code>var thread = threads.start(function(){
//do something
});
thread.waitFor();
thread.setTimeout(function(){
//do something
}, 1000);
</code></pre><h2>Thread.setTimeout(callback, delay[, ...args])<span><a class="mark" href="#threads_thread_settimeout_callback_delay_args" id="threads_thread_settimeout_callback_delay_args">#</a></span></h2>
<p>参见<a href="timers.html#timers_settimeout_callback_delay_args">timers.setTimeout()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<pre><code>log(&quot;当前线程(主线程):&quot; + threads.currentThread());
var thread = threads.start(function(){
//设置一个空的定时来保持线程的运行状态
setInterval(function(){}, 1000);
});
sleep(1000);
thread.setTimeout(function(){
log(&quot;当前线程(子线程):&quot; + threads.currentThread());
exit();
}, 1000);
</code></pre><h2>Thread.setInterval(callback, delay[, ...args])<span><a class="mark" href="#threads_thread_setinterval_callback_delay_args" id="threads_thread_setinterval_callback_delay_args">#</a></span></h2>
<p>参见<a href="timers.html#timers_setinterval_callback_delay_args">timers.setInterval()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<h2>Thread.setImmediate(callback[, ...args])<span><a class="mark" href="#threads_thread_setimmediate_callback_args" id="threads_thread_setimmediate_callback_args">#</a></span></h2>
<p>参见<a href="timers.html#timers_setimmediate_callback_delay_args">timers.setImmediate()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<h2>Thread.clearInterval(id)<span><a class="mark" href="#threads_thread_clearinterval_id" id="threads_thread_clearinterval_id">#</a></span></h2>
<p>参见<a href="timers.html#timers_clearinterval_id">timers.clearInterval()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<h2>Thread.clearTimeout(id)<span><a class="mark" href="#threads_thread_cleartimeout_id" id="threads_thread_cleartimeout_id">#</a></span></h2>
<p>参见<a href="timers.html#timers_cleartimeout_id">timers.clearTimeout()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<h2>Thread.clearImmediate(id)<span><a class="mark" href="#threads_thread_clearimmediate_id" id="threads_thread_clearimmediate_id">#</a></span></h2>
<p>参见<a href="timers.html#timers_clearimmediate_id">timers.clearImmediate()</a></p>
<p>区别在于, 该定时器会在该线程执行。如果当前线程仍未开始执行或已经执行结束,则抛出<code>IllegalStateException</code></p>
<h1>线程安全<span><a class="mark" href="#threads" id="threads">#</a></span></h1>
<p>线程安全问题是一个相对专业的编程问题,本章节只提供给有需要的用户。</p>
<p>引用维基百科的解释:</p>
<blockquote>
<p>线程安全是编程中的术语,指某个函数、函数库在多线程环境中被调用时,能够正确地处理多个线程之间的共享变量,使程序功能正确完成。</p>
</blockquote>
<p>在Auto.js中线程间变量在符合JavaScript变量作用域规则的前提下是共享的例如全局变量在所有线程都能访问并且保证他们在所有线程的可见性。但是不保证任何操作的原子性。例如经典的自增&quot;i++&quot;将不是原子性操作。</p>
<p>Rhino和Auto.js提供了一些简单的设施来解决简单的线程安全问题如锁<code>threads.lock()</code>, 函数同步锁<code>sync()</code>, 整数原子变量<code>threads.atomic()</code>等。</p>
<p>例如,对于多线程共享下的整数的自增操作(自增操作会导致问题,是因为自增操作实际上为<code>i = i + 1</code>也就是先读取i的值, 把他加1, 再赋值给i, 如果两个线程同时进行自增操作可能出现i的值只增加了1的情况),应该使用<code>threads.atomic()</code>函数来新建一个整数原子变量,或者使用锁<code>threads.lock()</code>来保证操作的原子性,或者用<code>sync()</code>来增加同步锁。</p>
<p>线程不安全的代码如下:</p>
<pre><code>var i = 0;
threads.start(function(){
while(true){
log(i++);
}
});
while(true){
log(i++);
}
</code></pre><p>此段代码运行后打开日志,可以看到日志中有重复的值出现。</p>
<p>使用<code>threads.atomic()</code>的线程安全的代码如下:</p>
<pre><code>//atomic返回的对象保证了自增的原子性
var i = threads.atomic();
threads.start(function(){
while(true){
log(i.getAndIncrement());
}
});
while(true){
log(i.getAndIncrement());
}
</code></pre><p>或者:</p>
<pre><code>//锁保证了操作的原子性
var lock = threads.lock();
var i = 0;
threads.start(function(){
while(true){
lock.lock();
log(i++);
lock.unlock();
}
});
while(true){
lock.lock();
log(i++);
lock.unlock();
}
</code></pre><p>或者:</p>
<pre><code>//sync函数会把里面的函数加上同步锁使得在同一时刻最多只能有一个线程执行这个函数
var i = 0;
var getAndIncrement = sync(function(){
return i++;
});
threads.start(function(){
while(true){
log(getAndIncrement());
}
});
while(true){
log(getAndIncrement());
}
</code></pre><p>另外数组Array不是线程安全的如果有这种复杂的需求请用Android和Java相关API来实现。例如<code>CopyOnWriteList</code>, <code>Vector</code>等都是代替数组的线程安全的类,用于不同的场景。例如:</p>
<pre><code>var nums = new java.util.Vector();
nums.add(123);
nums.add(456);
toast(&quot;长度为&quot; + nums.size());
toast(&quot;第一个元素为&quot; + nums.get(0));
</code></pre><p>但很明显的是,这些类不像数组那样简便易用,也不能使用诸如<code>slice()</code>之类的方便的函数。在未来可能会加入线程安全的数组来解决这个问题。当然您也可以为每个数组的操作加锁来解决线程安全问题:</p>
<pre><code>var nums = [];
var numsLock = threads.lock();
threads.start(function(){
//向数组添加元素123
numsLock.lock();
nums.push(123);
log(&quot;线程: %s, 数组: %s&quot;, threads.currentThread(), nums);
numsLock.unlock();
});
threads.start(function(){
//向数组添加元素456
numsLock.lock();
nums.push(456);
log(&quot;线程: %s, 数组: %s&quot;, threads.currentThread(), nums);
numsLock.unlock();
});
//删除数组最后一个元素
numsLock.lock();
nums.pop();
log(&quot;线程: %s, 数组: %s&quot;, threads.currentThread(), nums);
numsLock.unlock();
</code></pre><h2>sync(func)<span><a class="mark" href="#threads_sync_func" id="threads_sync_func">#</a></span></h2>
<div class="signature"><ul>
<li><code>func</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 函数</li>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a></li>
</ul>
</div><p>给函数func加上同步锁并作为一个新函数返回。</p>
<pre><code>var i = 0;
function add(x){
i += x;
}
var syncAdd = sync(add);
syncAdd(10);
toast(i);
</code></pre><h1>线程通信<span><a class="mark" href="#threads_1" id="threads_1">#</a></span></h1>
<p>Auto.js提供了一些简单的设施来支持简单的线程通信。<code>threads.disposable()</code>用于一个线程等待另一个线程的(一次性)结果,同时<code>Lock.newCondition()</code>提供了Condition对象用于一般的线程通信(await, signal)。另外,<code>events</code>模块也可以用于线程通信,通过指定<code>EventEmiiter</code>的回调执行的线程来实现。</p>
<p>使用<code>threads.disposable()</code>可以简单地等待和获取某个线程的执行结果。例如要等待某个线程计算&quot;1+.....+10000&quot;:</p>
<pre><code>var sum = threads.disposable();
//启动子线程计算
threads.start(function(){
var s = 0;
//从1加到10000
for(var i = 1; i &lt;= 10000; i++){
s += i;
}
//通知主线程接收结果
sum.setAndNotify(s);
});
//blockedGet()用于等待结果
toast(&quot;sum = &quot; + sum.blockedGet());
</code></pre><p>如果上述代码用<code>Condition</code>实现:</p>
<pre><code>//新建一个锁
var lock = threads.lock();
//新建一个条件,即&quot;计算完成&quot;
var complete = lock.newCondition();
var sum = 0;
threads.start(function(){
//从1加到10000
for(var i = 1; i &lt;= 10000; i++){
sum += i;
}
//通知主线程接收结果
lock.lock();
complete.signal();
lock.unlock();
});
//等待计算完成
lock.lock();
complete.await();
lock.unlock();
//打印结果
toast(&quot;sum = &quot; + sum);
</code></pre><p>如果上诉代码用<code>events</code>模块实现:</p>
<pre><code>//新建一个emitter, 并指定回调执行的线程为当前线程
var sum = events.emitter(threads.currentThread());
threads.start(function(){
var s = 0;
//从1加到10000
for(var i = 1; i &lt;= 10000; i++){
s += i;
}
//发送事件result通知主线程接收结果
sum.emit(&#39;result&#39;, s);
});
sum.on(&#39;result&#39;, function(s){
toastLog(&quot;sum = &quot; + s + &quot;, 当前线程: &quot; + threads.currentThread());
});
</code></pre><p>有关线程的其他问题例如生产者消费者等问题请用Java相关方法解决例如<code>java.util.concurrent.BlockingQueue</code></p>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -72,12 +72,12 @@
<h2>目录</h2> <h2>目录</h2>
<ul> <ul>
<li><span class="stability_2"><a href="#timers_timers">Timers</a></span><ul> <li><span class="stability_2"><a href="#timers_timers">Timers</a></span><ul>
<li><span class="stability_undefined"><a href="#timers_setimmediate_callback_args">setImmediate(callback[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#timers_setinterval_callback_delay_args">setInterval(callback, delay[, ...args])</a></span></li> <li><span class="stability_undefined"><a href="#timers_setinterval_callback_delay_args">setInterval(callback, delay[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#timers_settimeout_callback_delay_args">setTimeout(callback, delay[, ...args])</a></span></li> <li><span class="stability_undefined"><a href="#timers_settimeout_callback_delay_args">setTimeout(callback, delay[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#timers_clearimmediate_id">clearImmediate(id)</a></span></li> <li><span class="stability_undefined"><a href="#timers_setimmediate_callback_args">setImmediate(callback[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#timers_clearinterval_id">clearInterval(id)</a></span></li> <li><span class="stability_undefined"><a href="#timers_clearinterval_id">clearInterval(id)</a></span></li>
<li><span class="stability_undefined"><a href="#timers_cleartimeout_id">clearTimeout(id)</a></span></li> <li><span class="stability_undefined"><a href="#timers_cleartimeout_id">clearTimeout(id)</a></span></li>
<li><span class="stability_undefined"><a href="#timers_clearimmediate_id">clearImmediate(id)</a></span></li>
</ul> </ul>
</li> </li>
</ul> </ul>
@@ -88,14 +88,25 @@
<h1>Timers<span><a class="mark" href="#timers_timers" id="timers_timers">#</a></span></h1> <h1>Timers<span><a class="mark" href="#timers_timers" id="timers_timers">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>timers 模块暴露了一个全局的 API用于在某个未来时间段调用调度函数。 因为定时器函数是全局的,所以使用该 API 无需调用 timers.<em>*</em></p> <div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>timers 模块暴露了一个全局的 API用于在某个未来时间段调用调度函数。 因为定时器函数是全局的,所以使用该 API 无需调用 timers.<em>*</em></p>
<p>Auto.js 中的计时器函数实现了与 Web 浏览器提供的定时器类似的 API除了它使用了一个不同的内部实现它是基于 Android Looper-Handler消息循环机制构建的。其实现机制与Node.js比较相似。</p> <p>Auto.js 中的计时器函数实现了与 Web 浏览器提供的定时器类似的 API除了它使用了一个不同的内部实现它是基于 Android Looper-Handler消息循环机制构建的。其实现机制与Node.js比较相似。</p>
<h2>setImmediate(callback[, ...args])<span><a class="mark" href="#timers_setimmediate_callback_args" id="timers_setimmediate_callback_args">#</a></span></h2> <p>例如要在5秒后发出消息&quot;hello&quot;:</p>
<div class="signature"><ul> <pre><code>setTimeout(function(){
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 在Looper循环的当前回合结束时要调用的函数。</li> toast(&quot;hello&quot;)
<li><code>...args</code> <span class="type">&lt;any&gt;</span> 当调用 callback 时要传入的可选参数。</li> }, 5000);
</ul> </code></pre><p>需要注意的是,这些定时器仍然是单线程的。如果脚本主体有耗时操作或死循环,则设定的定时器不能被及时执行,例如:</p>
</div><p>预定立即执行的 callback它是在 I/O 事件的回调之后被触发。 返回一个用于 clearImmediate() 的 id。</p> <pre><code>setTimeout(function(){
<p>当多次调用 setImmediate() 时callback 函数会按照它们被创建的顺序依次执行。 每次事件循环迭代都会处理整个回调队列。 如果一个立即定时器是被一个正在执行的回调排入队列的,则该定时器直到下一次事件循环迭代才会被触发。</p> //这里的语句会在15秒后执行而不是5秒后
<h2>setInterval(callback, delay[, ...args])<span><a class="mark" href="#timers_setinterval_callback_delay_args" id="timers_setinterval_callback_delay_args">#</a></span></h2> toast(&quot;hello&quot;)
}, 5000);
//暂停10秒
sleep(10000);
</code></pre><p>再如:</p>
<pre><code>setTimeout(function(){
//这里的语句永远不会被执行
toast(&quot;hello&quot;)
}, 5000);
//死循环
while(true);
</code></pre><h2>setInterval(callback, delay[, ...args])<span><a class="mark" href="#timers_setinterval_callback_delay_args" id="timers_setinterval_callback_delay_args">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 当定时器到点时要调用的函数。</li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 当定时器到点时要调用的函数。</li>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 调用 callback 之前要等待的毫秒数。</li> <li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 调用 callback 之前要等待的毫秒数。</li>
@@ -112,22 +123,38 @@
</div><p>预定在 delay 毫秒之后执行的单次 callback。 返回一个用于 clearTimeout() 的 id。</p> </div><p>预定在 delay 毫秒之后执行的单次 callback。 返回一个用于 clearTimeout() 的 id。</p>
<p>callback 可能不会精确地在 delay 毫秒被调用。 Auto.js 不能保证回调被触发的确切时间,也不能保证它们的顺序。 回调会在尽可能接近所指定的时间上调用。</p> <p>callback 可能不会精确地在 delay 毫秒被调用。 Auto.js 不能保证回调被触发的确切时间,也不能保证它们的顺序。 回调会在尽可能接近所指定的时间上调用。</p>
<p>当 delay 小于 0 时delay 会被设为 0。</p> <p>当 delay 小于 0 时delay 会被设为 0。</p>
<h2>setImmediate(callback[, ...args])<span><a class="mark" href="#timers_setimmediate_callback_args" id="timers_setimmediate_callback_args">#</a></span></h2>
<div class="signature"><ul>
<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a> 在Looper循环的当前回合结束时要调用的函数。</li>
<li><code>...args</code> <span class="type">&lt;any&gt;</span> 当调用 callback 时要传入的可选参数。</li>
</ul>
</div><p>预定立即执行的 callback它是在 I/O 事件的回调之后被触发。 返回一个用于 clearImmediate() 的 id。</p>
<p>当多次调用 setImmediate() 时callback 函数会按照它们被创建的顺序依次执行。 每次事件循环迭代都会处理整个回调队列。 如果一个立即定时器是被一个正在执行的回调排入队列的,则该定时器直到下一次事件循环迭代才会被触发。</p>
<p>setImmediate()、setInterval() 和 setTimeout() 方法每次都会返回表示预定的计时器的id。 它们可用于取消定时器并防止触发。</p> <p>setImmediate()、setInterval() 和 setTimeout() 方法每次都会返回表示预定的计时器的id。 它们可用于取消定时器并防止触发。</p>
<h2>clearInterval(id)<span><a class="mark" href="#timers_clearinterval_id" id="timers_clearinterval_id">#</a></span></h2>
<div class="signature"><ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setInterval() 返回的 id。</li>
</ul>
</div><p>取消一个由 setInterval() 创建的循环定时任务。</p>
<p>例如:</p>
<pre><code>//每5秒就发出一次hello
var id = setInterval(function(){
toast(&quot;hello&quot;);
}, 5000);
//1分钟后取消循环
setTimeout(function(){
clearInterval(id);
}, 60 * 1000);
</code></pre><h2>clearTimeout(id)<span><a class="mark" href="#timers_cleartimeout_id" id="timers_cleartimeout_id">#</a></span></h2>
<div class="signature"><ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setTimeout() 返回的 id。</li>
</ul>
</div><p>取消一个由 setTimeout() 创建的定时任务。</p>
<h2>clearImmediate(id)<span><a class="mark" href="#timers_clearimmediate_id" id="timers_clearimmediate_id">#</a></span></h2> <h2>clearImmediate(id)<span><a class="mark" href="#timers_clearimmediate_id" id="timers_clearimmediate_id">#</a></span></h2>
<div class="signature"><ul> <div class="signature"><ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setImmediate() 返回的 id。</li> <li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setImmediate() 返回的 id。</li>
</ul> </ul>
</div><p>取消一个由 setImmediate() 创建的 Immediate 对象。</p> </div><p>取消一个由 setImmediate() 创建的 Immediate 对象。</p>
<h2>clearInterval(id)<span><a class="mark" href="#timers_clearinterval_id" id="timers_clearinterval_id">#</a></span></h2>
<div class="signature"><ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setInterval() 返回的 id。</li>
</ul>
</div><p>取消一个由 setInterval() 创建的 Timeout 对象。</p>
<h2>clearTimeout(id)<span><a class="mark" href="#timers_cleartimeout_id" id="timers_cleartimeout_id">#</a></span></h2>
<div class="signature"><ul>
<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 一个 setTimeout() 返回的 id。</li>
</ul>
</div><p>取消一个由 setTimeout() 创建的 Timeout 对象。</p>
</div> </div>
</div> </div>