以on{X}平台用JavaScript寫Android自動化程式

最近看了不少使用Android的先進介紹好用的工具,其中有個被稱為「神器」的Tasker是被很多人提及的一個自動化工具,它能設定各種情況下能自動做處理的規則;原來Android類似的程式還有不少,如LlamaAutomateItMyProfiles(台灣人的作品)等。這些程式大致都是作者提供各式的狀況設定,讓手機用戶能指定遇到那種狀況要使用那種處理,但沒想到微軟的一個以色列團隊在今年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}網頁,網頁左上方是建立與維護腳本的區域,其下方是開發團隊的版本更新訊息,右方是論壇的最近討論。

onx.ms page

左上方的腳本功能區主要有下列按鈕功能:

  • 【create】:出現編輯腳本的網頁,寫好程式後按【save and send to phone】,程式會被推到手機的on{X} App,並自動執行此腳本。程式碼網頁還有一個logs分頁,能顯示腳本執行後的記錄訊息(顯示在手機上的log會自己寫回網站在此顯示)
    onx create
  • 【recipes】(食譜)網頁裡則有蒐集好的許多腳本樣本,可以拿來當做樣板再修改成自己需要的內容。
  • 【view all logs】:腳本可以用console.log寫出訊息,此功能會顯示所有腳本的訊息
    onx logs

GPS與經緯度

我用一個簡單的情景來示範on{X}的威力:當我們回到家裡後,讓手機自動打開WiFi並關閉3G。

手機要如何知道我們回家了呢?最直覺的作法是用GPS判斷經緯度,只要進入家裡經緯度範圍內就知道回家了。取得經緯度的方法列舉下列兩種:

  1. 用Google Maps查詢你家地址後按左上方的連結,複製彈出的網址,找「amp;ll=」後面的兩個數值,第一個是緯度,第二個是經度
    Google maps
  2. 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}也就無法使用,分數自然也高不起來。

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *