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

Power Management

非標準

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

摘要

Power Management API 可管理裝置的耗電情形。

管理電力

電力管理與電力本身不盡相同,如避免大量耗電、限制回流 (Reflow) 等等,都屬於電源管理的一部分。而 Power Management API 則直接控管如 CPU 與螢幕等的耗電量。透過 navigator.mozPower 即可存取電力管理的主要介面。而 navigator.mozPower 則為 PowerManager 介面的實例 (Instance)。

基本電力操作

PowerManager 介面可管理基本的電力操作情形。

通用電力操作

powerOff() 函式可關閉裝置;reboot() 函式則可重新開機。

navigator.mozPower.powerOff();

螢幕電力操作

透過可讀寫的 screenEnabled 屬性即可開/關螢幕畫面;而存取/更改畫面亮度也能達到相同效果。如可讀寫的 screenBrightness (用以定義螢幕背光的亮度) 就能變更畫面亮度,可調整為 0 (全暗) 到 1 (全亮)。

// It doesn't make sense to change the brightness if the screen is off
if (navigator.mozPower.screenEnabled) {
  navigator.mozPower.screenBrightness = 0.5;
}

CPU 電力操作

目前並無法直接關閉 CPU。但可進一步設定 cpuSleepAllowed,以決定關閉畫面之時是否要關閉 CPU。在畫面關閉 (true) 或開啟 (false) 時,此函式可定義是否讓裝置的 CPU 休眠。若畫面處於開啟狀態 (false),則該函式將避免裝置進入靜止狀態。

進階電力操作

若管理電力的 Apps 可確實接收到第三方 Apps 的需求通知,就能達到更好的電力管理效果。舉例來說,在使用者欣賞影片時,當然不希望系統在幾分鐘之後自動關閉畫面。

請求 Wake locks

任何 Apps 均可請求 Wake locks,避免裝置切斷 Apps 本身所需資源的電力。可透過 navigator.requestWakeLock() 函式請求 Wake locks。

特定資源所要求的 Wake locks,可能會因不同的理由而取消。舉例來說,行動裝置的電力管理功能,可能會在閒置一段時間之後決定關閉畫面,進而達到省電目的。在關閉相關資源之前,處理資源的 Apps 往往會檢查相關資源的鎖定狀態。再舉個例子,某個頁面可能持續 screen 上的鎖定狀態,以避免畫面關閉或螢幕保護程式運作。

依預設值,Firefox OS 允許 screen、cpu、wifi 資源。但只要是處理資源的任何 App 均可定義資源名稱,並針對該鎖定功能設定規則。舉例來說,資源管理功能可無視 Apps 對 screen 所進行的 Wake locks,讓 Apps 無法插手鎖定狀態。

var lock = navigator.requestWakeLock('screen');

requestWakeLock 函式所回傳的物件將包含 topic 屬性,代表目前要鎖定的資源。而 unlock() 函式可供手動解鎖。另請注意,若 App 進入關閉狀態 (真的關閉,而不是閒置),就會自動送出該 App 需要的所有鎖定請求。

處理 Wake locks

只要是可管理鎖定的 Certified Apps,均可得知鎖定狀態的變化。其實任何管理電力的 Apps 均應監聽 screencpu 鎖定狀態的變化。透過 PowerManager.addWakeLockListener() 函式即可達到此目的。另可透過 PowerManager.removeWakeLockListener() 函式而停止監聽鎖定請求。

addWakeLockListener 函式所接收的回呼 (Callback) 需具備 2 組參數。第一組字串代表應處理的資源 (本範例為 screencpu);第二組字串則代表鎖定的狀態。

鎖定可分為 3 種狀態:

unlocked
沒有任何 App 針對既有資源持續 Wake lock。
locked-foreground
至少 1 個 App 持續 Wake lock,且於前景顯示該 App。
locked-background
至少 1 個 App 持續 Wake lock,但所有 Apps 均在背景而未顯示。
// This is used to keep track of the last change on the lock state
var screenTimeout;

// A reference to the Power Manager
var power = window.navigator.mozPower;

// Here are the actions to handle based on the lock state
var powerAction = {

  // If there is no lock at all, we will suspend the device:
  // * Turn the screen off
  // * Allow the cpu to shut down
  unlocked: function suspendDevice() {
    power.cpuSleepAllowed = true;
    power.screenEnabled   = false;
  },
 
  // If there is a lock but the applications requesting it
  // are all in the background, we just turn the screen off
  'locked-background': function shutOffOnlyScreen() {
    power.cpuSleepAllowed = false;
    power.screenEnabled   = false;
  },

  // At last, if there is an active application that requests a lock,
  // actually there is nothing to do. That's the whole point.
}
 
function screenLockListener(topic, state) {
  // If the lock is not about the screen, there is nothing to do.
  if ('screen' !== topic) return;

  // Each time the lock changes state, we stop any pending power management operations
  window.clearTimeout(screenTimeout);

  // If there is an action defined for the given state
  if (powerAction[state]) {
    // We delay that action by 3s
    screenTimeout = window.setTimeout(powerAction[state], 3000);
  }
}

// We make sure our power management application is listening for any change on locks.
power.addWakeLockListener(screenLockListener);

規格

尚無任何規格。

另可參閱

文件標籤與貢獻者

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