Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Idle

非標準

This API is available on Firefox OS for certified applications only.

摘要

當使用者讓裝置進入閒置/待機狀態,可通知 App 以採取相對應的行動。最常見的情形就是待機省電。而待機省電狀態又可另外搭配 Power Management API

Idle Observer

若要讓任一 App 收到「系統閒置」的通知,就必須先註冊 Idle Observer。而 Idle Observer 物件具備了 3 項屬性:

  • time 屬性將定義使用者最後的動作之後,理應進入閒置/待機狀態的時間 (以秒計算)。
  • onidle 屬性則是判斷使用者閒置之後,所會呼叫的函式。
  • onactive 屬性則是使用者再度啟動裝置之後,所會呼叫的函式。
// NOTE: mozPower is part of the Power Management API

var fadeLight = {
  time: 10, // Ten seconds

  onidle: function () {
    // The user does not seem active, let's dim the screen down
    navigator.mozPower.screenBrightness = 0.5;
  },

  onactive: function () {
    // Ok, the user is back, let's brighten the screen up
    navigator.mozPower.screenBrightness = 1;
  }
}

var screenOff = {
  time: 15, // fifteen seconds

  onidle: function () {
    // Ok, the user had his chance but he's really idle, let's turn the screen off
    navigator.mozPower.screenEnabled = false;
  },

  onactive: function () {
    // Ok, the user is back, let's turn the screen on
    navigator.mozPower.screenEnabled = true;
  }
}

任一 App 均可依需要而定義為多個 Idle Observer。但不論定義成幾個 Idle Observer,都必須以 navigator.addIdleObserver() 函式完成註冊,才能由系統所處理。

navigator.addIdleObserver(fadeLight);
navigator.addIdleObserver(screenOff);

又如果 App 不再需要知道使用者是否閒置了系統,則可透過 navigator.removeIdleObserver() 函式移除 Idle Observer。

navigator.removeIdleObserver(fadeLight);
navigator.removeIdleObserver(screenOff);

規格

目前仍未有任何規格,但 W3C 將此 API 納入為 System Applications Working Group 討論的一部分。

另可參閱

文件標籤與貢獻者

 此頁面的貢獻者: MashKao
 最近更新: MashKao,