以on{X}平台用JavaScript寫Android自動化程式
最近看了不少使用Android的先進介紹好用的工具,其中有個被稱為「神器」的Tasker是被很多人提及的一個自動化工具,它能設定各種情況下能自動做處理的規則;原來Android類似的程式還有不少,如Llama、AutomateIt、MyProfiles(台灣人的作品)等。這些程式大致都是作者提供各式的狀況設定,讓手機用戶能指定遇到那種狀況要使用那種處理,但沒想到微軟的一個以色列團隊在今年6月推出一個名為on{X}(讀作on-ex)的應用程式,能讓我們以JavaScript透過它內建的元件來控制手機的各種行為,從6月到8月30日持續有新版更新,提供的元件也越來越多,不用寫Java程式而直接以相對簡單的JavaScript腳本控制手機,實在是相當有趣,如果on{X}更穩定、更省電,只要能發揮想像力,那麼能展現的功能必定更強大。
以下簡單介紹建立on{X}腳本(稱為rules)的步驟。
建立第一個on{X}腳本
首先必須以Facebook帳號登入on{X}網站並授權on{X}讀取Facebook的基本資料後,就能進入你專屬的on{X}網頁,網頁左上方是建立與維護腳本的區域,其下方是開發團隊的版本更新訊息,右方是論壇的最近討論。
左上方的腳本功能區主要有下列按鈕功能:
- 【create】:出現編輯腳本的網頁,寫好程式後按【save and send to phone】,程式會被推到手機的on{X} App,並自動執行此腳本。程式碼網頁還有一個logs分頁,能顯示腳本執行後的記錄訊息(顯示在手機上的log會自己寫回網站在此顯示)
- 【recipes】(食譜)網頁裡則有蒐集好的許多腳本樣本,可以拿來當做樣板再修改成自己需要的內容。
- 【view all logs】:腳本可以用console.log寫出訊息,此功能會顯示所有腳本的訊息
GPS與經緯度
我用一個簡單的情景來示範on{X}的威力:當我們回到家裡後,讓手機自動打開WiFi並關閉3G。
手機要如何知道我們回家了呢?最直覺的作法是用GPS判斷經緯度,只要進入家裡經緯度範圍內就知道回家了。取得經緯度的方法列舉下列兩種:
- 用Google Maps查詢你家地址後按左上方的連結,複製彈出的網址,找「amp;ll=」後面的兩個數值,第一個是緯度,第二個是經度
- 用http://www.getlatlon.com/輸入地址後查詢是更簡單的方法,直接會在網頁下方顯示經緯度
on{x}最上層用來表示手機(或平板)設備物件是device,經緯度地理位置要建立一個「地區」(region)物件:
var location = {name: "Home", latitude: 25.049845, longitude: 121.580291, radius: 50}; var region = device.regions.createRegion(location);
進入指定地區會觸發enter事件,離開指定地區則觸發exit事件。觸發程式範例如下:
device.regions.on("enter", function (signal) { console.log(' enter the region:' + signal.latitude + ',' + signal.longitude); device.network.mobileDataEnabled = false; // Turn off 3G device.network.wifiEnabled = true; // Turn on Wifi }); device.regions.on("exit", function (signal) { console.log(' exit the region:' + signal.latitude + ',' + signal.longitude); device.network.mobileDataEnabled = true; device.network.wifiEnabled = false; });
在程式最後加上監控位置的程式碼:
device.regions.startMonitoring(region);
為了確認程式有執行,我們可以用下列寫法輸出訊息到通知區域:
var notification = device.notifications.createNotification( 'Turn on Home WiFi (' + location.latitude + "," + location.longitude + ')'); notification.show();
完整的程式
var location = {name: "Home", latitude: 25.049845, longitude: 121.580291, radius: 50}; // remove old regions device.regions.stopMonitoringAll(); //console.log('Turn on WiFi at (' + location.latitude + "," + location.longitude + ')'); var notification = device.notifications.createNotification('Turn on Home WiFi at (' + location.latitude + "," + location.longitude + ')'); notification.show(); // create a geo region for the trigger to take place at var region = device.regions.createRegion(location); // enable/disable bluetooth when entering/leaving the region device.regions.on("enter", function (signal) { console.log(' enter the region:' + signal.latitude + ',' + signal.longitude); device.network.mobileDataEnabled = false; // Turn off 3G device.network.wifiEnabled = true; // turn on Wifi }); device.regions.on("exit", function (signal) { console.log(' exit the region:' + signal.latitude + ',' + signal.longitude); device.network.mobileDataEnabled = true; device.network.wifiEnabled = false; }); device.regions.startMonitoring(region); console.log('Completed Script:' + device.currentSource); // Test code: //device.regions.emit('enter',{"latitude" : 25.049845,"longitude" : 121.580291});
on{X}相關連結
還有千萬不要被on{X}偏低的評分騙了,有許多低分是對岸用戶評的,因為大陸地區無法使用必要登入的Facebook,on{X}也就無法使用,分數自然也高不起來。
- 官方網站:https://onx.ms
- 官方論壇:https://dev.onx.ms/forum/
- on{X}的第三方市場:http://onxmarket.com/
##
您可能也會有興趣的類似文章
- on{X}: 偵測經緯度與使用emit模擬觸發事件 (0則留言, 2012/09/05)
- Android Studio的安裝、設定與執行 (4則留言, 2013/10/03)
- 開發Android應用程式時操作SQLite資料庫的方法 (0則留言, 2019/03/30)
- 變更MyBlogLog的onclick事件處理程式 (V1.3修改) (8則留言, 2007/04/08)
- [Android Studio #2] 操作資源XML檔 (0則留言, 2013/10/03)
- [Android] 「滑動Home鍵」輕鬆設定8種手勢 (2則留言, 2013/08/01)
- 用jQuery.formError取代alert( ) (0則留言, 2016/11/21)
- Galaxy S3 Root步驟簡述與擷取手機螢幕Apps (0則留言, 2012/08/11)
- [Android Studio #3] 使用Android Asset Studio產生圖示檔與@+id與@id之差異 (0則留言, 2013/10/07)
- jQuery DataTables範例1:啟始設定 (0則留言, 2011/10/20)
- [jQuery元件] Fancybox 3使用備忘 (0則留言, 2018/08/26)
- [Android Studio #9] 無法執行x86模擬器的處理步驟 (2則留言, 2015/04/20)
- 把文章的分類當做HEMiDEMi的標籤:Xuite自動書籤按鈕 V1.3 (2則留言, 2007/02/27)
- 初試AJAX:中文的傳遞與接收 (0則留言, 2007/03/16)
- 修改自動產生分享書籤的按鈕 (4則留言, 2007/02/11)
Sorry, 很久沒用Google sc…