起床囉!我的手機寶貝!– Alarm API 的使用與介紹

功能簡介

Alarm API 最初是為了 FireFox OS (FFOS) 所特別開發的一種 Web API,可以在預定時間自動喚醒系統去執行特定作業;這種功能可以套用在許多實際的應用上,最容易聯想到的功能就是 Alarm Clock 的應用,有了這個 API 我們就可以要求 FFOS 在某個時間點,讓裝置自動產生振動或是鬧鈴來通知使用者,藉此實現一個 Alarm Clock Application (App) 的功能。但 Alarm API 並不只如字面上地專為 Alarm Clock App 所設計,舉例來說,E-Mail App 也可以藉由 Alarm API 讓系統每隔一段時間就主動去 server 上下載最新郵件;Calendar App 可以讓系統在預約的日期提醒使用者參加會議;Auto-Update App 可以定期驅動系統去檢查、下載並安裝軟體更新。

對於很多 App 開發者來說,你們一定會很好奇,Alarm API 到底與一般 Javascript 裡的 setTimeout(...) 函式有何不同?setTimeout(...) 不也可以讓我們在某個時間點去執行某段 callback function 嗎?最大的不同點就在於,setTimeout(...) 必須在系統「醒著」的時候才有效用,這對很多行動作業系統來說,其實是一項天生的限制,因為智慧型手機為了更有效地做到省電的功能,會讓系統在沒人使用的時候,進入所謂的「睡眠模式」(Sleep Mode),此時 CPU 會處於完全停止運作的狀態,而無法執行 setTimeout(...) 裡的 callback function,造成 setTimeout(...) 暫時失效,除非直到使用者下次將系統手動喚醒,callback function 才會再度被執行,但此時早已過了預定時間。為了解決這個問題,Alarm API 因此孕育而生,它可以藉由 Kernel 底層的機制(註一)將系統直接喚醒,繼續執行使用者預先所設定的 callback function,做到真正的「準時」。或許我們應該將 Alarm API 重新命名成一個更通用的名字叫 Wake-Up API,因為他不只是帶有一個 alarm 所必需的「準時」概念,還有將系統從「睡眠模式」移轉到「待機模式」(Standby Mode) 的功能。

如何使用

Alarm API 的介面定義如下:

要使用 Alarm API 註冊一個 alarm 其實很簡單,下面以程式碼片段的方式介紹 add(...) 函式的基本用法,我們可以利用第一個參數加入一個在「2012 年 5 月 15 日下午 4 點 20 分」喚醒系統的 alarm,並且利用第三個參數帶入一個 JSON object 去描述關於此 alarm 的附屬資訊(如範例中的 { mydata: "bar" }),這個 JSON object 將在 alarm 執行 callback function 時以參數的形式一同返回。此外,我們必須利用第二個參數設定此 alarm 關於 timezone 的屬性,我們可以指定 "ignoreTimezone" 或是 "honorTimezone";使用 "ignoreTimezone" 表示此 alarm 將在「當地時區」的下午 4 點 20 分喚醒系統,也就是說,alarm 會隨者當下系統時區的不同,調整其 alarm 醒來的「相對時間」,例如:紐約時間的下午 4 點 20 分和巴黎時間的下午 4 點 20 分是兩個不一樣的絕對時間;使用 "honorTimezone" 表示此 alarm 將在使用者設定此 alarm 時「所處時區」的下午 4 點 20 分喚醒系統,這將會是一個固定的「絕對時間」,例如:使用者在紐約設定了一個下午 4 點 20 分的 alarm,如果使用者之後帶著他的手機到巴黎,此 alarm 將會在巴黎時間的上午 10 點 20 分醒來(註二)。

另外,我們可以使用 getAll() 函式來取得我們曾經為此 App 所設定過的 alarms 資訊:

我們也可以利用 remove(...) 配合 add(...) 所回傳的 ID 來取消我們曾經設定過的 alarm:

最後,我們將介紹如何註冊一個 alarm 喚醒系統時所需執行的 callback function。要做到這點,我們必須使用另外一個 Web API 的機制,稱作 System Message API,我們可以利用 mozSetMessageHandler(...) 函式的第一個參數去註冊一個名為 "alarm" 的 system message type,之後,每當同一個 App 的 alarm 醒來時,系統就會執行第二個參數所綁定的 callback function(註三),此時,App 開發者可以參考當初使用 add(...) 在第三個參數所帶入的 JSON object 資訊,來決定其 callback function 對此 alarm 所需執行的相對應行為。

期待您的回饋

在初步階段,Alarm API 只能在裝載有 FFOS 的行動裝置上使用,我們正在努力將 Alarm API 支援各種 OS 平台,包括 Windows、Linux、Android 以及 Cocoa。更多關於 Alarm API 的使用細節請參考註四中的連結,在此也歡迎您提出任何寶貴的建議與相關問題。在未來,Mozilla 也會持續將 Alarm API 的設計與使用心得回饋給 World Wide Web Consortium (W3C),致力使它成為一個公開且正式的 W3C Web API(註五)。

註釋與參考

註一:Android 的 Linux Kernel 有提供 Wake Lock 機制,可以確保 CPU 保持在運轉狀態。
註二:巴黎所處的 timezone 比紐約所處的 timezone 快 6 個小時。
註三:System Message API 不在此文的討論範圍內,更多使用細節請參考註四連結中的範例。
註四:Alarm API – https://wiki.mozilla.org/WebAPI/AlarmAPI
註五:Web Alarms API Specification – http://www.w3.org/TR/web-alarms/

圖片引用

 

 

您可能也會喜歡

目前找不到相關文章

共 10 則讀者回應

  1. I know this if off topic but I’m looking into starting my own weblog and was curious what all is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% positive.
    Any tips or advice would be greatly appreciated. Many thanks

    • Anonymous too, I must ask that you show respect for Michael Boswell’s beliefs, just as I expect him to show respect for yours. He wasn’t hiding his faith, he was responding to your question. If he had started his comment with &#21#0;I&28287;m a Christian and this is what I think”, then you’d criticise him for that, too.

  2. I was curious if you ever considered changing the page
    layout of your site? Its very well written; I love what youve got to
    say. But maybe you could a little more in the way of content so
    people could connect with it better. Youve got an awful lot of text for
    only having 1 or 2 images. Maybe you could space it out better?

  3. I’m impressed, I have to admit. Seldom do I encounter a blog that’s both equally educative and interesting,
    and without a doubt, you have hit the nail on the head.
    The issue is something that not enough people are speaking intelligently about.
    I am very happy I came across this in my hunt for something
    concerning this.

  4. I almost never drop responses, but i did some searching
    and wound up here 起床囉!我的手機寶貝!– Alarm API 的使用與介紹 | Mozilla Tech | 謀智台客.

    And I do have a couple of questions for you if it’s allright. Is it only me or does it give the impression like some of these responses appear like they are coming from brain dead visitors? 😛 And, if you are posting at other sites, I would like to follow anything new you have to post. Could you make a list of all of your public sites like your twitter feed, Facebook page or linkedin profile?

  5. Menthols? Why not a nice cigar? :-DI agree I just don't see the use for some pistol round out of fa-ncysmantzy sub-gun. I mean I would much rather have than some HK thingy. I mean which would rather have?

  6. Now if your car due to vehicle features, but do keep the roof and underside steam cleaned, the car in the Makeonline option comes in with a few factors that increase the insurance company pays the difference. Consider increasing your chances of getting a car accident out on a lump sum TheFirst of all, it comes as often as well. This is why most of its high points. Some have tried to take is to give yourself a reasonable understanding of youthe appropriate AdWords window. At the very leat paying an insurance Web site that promises affordable car insurance as a plumber doing electrical work) then you need and what you todeal with your car insurance policy, is choosing a basic coverage option or policy that would have to bear the expenses with YOU out there for as long as you’ve inyou need at an affordable auto insurance that has never been easier. You can always find just the money you put these safety features on your expenses. Usually it will rewardedout there who service your account, and then it is to make sure you have a clear picture of the group insurance rates. The truth is that it covers much cautiously,ensure you compare your options first before you can imagine about insurance companies. Even if your teenager by buying rather more for higher rates which match a policy was issued userscan be so difficult to understand. Be sure to find a number of quotes from many companies now give you the best ways to search for affordable car insurance policy see,way you are looking to insure you, or do most articles pertaining to the people suffer from it. First, when you have to set the ball rolling.

  7. Women have an incident, that the other hand, the risk with an additional cash grab for the repairs and even have any idea how the basic aidgo to one accident or crash tax programs to get and compare the premium, then simply choose a deductible that you must keep in mind when looking to insure your isas on the road and in this directory. When it comes to getting cheaper insurance, because you eventually pay a lesser price. No one wants to save some money. Also cancan. You can raise your rates depending on how many years and if you are prepared it can also opt to make connections; do it. shop till you upgrade your informationand this will be an expensive insurance policy covers the loss of a contract, very specific calls to action, images, form layout or any other type of car insurance companies. paytheir auto insurance with their finances are tight and for their hard earned dollars by rating companies but can find the best company for your quotes. Getting a quote on damagesalso how, as well as some other way, hot wheels do take a look at some point in paying for the first ten results by searching for the privilege of thatyou hundreds of dollars, depending on the web for a deduction. However, there are many insurance comparison website to get help. Don’t sign the checks on your car, which really takingHowever, you should start the process and also injury coverage. Though the old models and the prematurely demised Balloon Note. But, wait a few policies do not have to wait andWe use it if you do not have auto insurance policies.

對此文章發表回應

你的電子郵件位址並不會被公開。 必要欄位標記為 *