電源とハードウェアの監視
ElectronのpowerMonitorモジュールでネイティブな電源・ハードウェアイベントを利用し、応答性が高くバッテリーを意識したデスクトップアプリを構築します。
「電源とハードウェアの監視」はCoddyKit上の無料Electron Desktop App Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElectron Desktop App Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Apps That Sense Their Environment
Great native apps react to power and hardware state: pausing work on battery, saving before sleep, resuming on wake.
The powerMonitor Module
Electron's powerMonitor in the main process emits system power events and reports battery state.
const { powerMonitor } = require('electron');
app.whenReady().then(() => {
console.log(powerMonitor.getSystemIdleState(60));
});Suspend and Resume
Listen for suspend and resume to flush data before sleep and refresh after wake.
powerMonitor.on('suspend', () => saveState());
powerMonitor.on('resume', () => reconnect());Battery vs AC Power
The on-battery and on-ac events let you throttle background work to save battery.
function syncInterval(onBattery) {
return onBattery ? 300000 : 60000;
}
console.log(syncInterval(true));Detecting Idle Time
getSystemIdleTime() returns seconds since the last user input, useful for auto-lock or away status.
function isAway(idleSeconds) {
return idleSeconds > 300;
}
console.log(isAway(420));Idle State
getSystemIdleState(threshold) reports 'active', 'idle', 'locked', or 'unknown' in one call.
Screen Lock Events
The lock-screen and unlock-screen events help you secure sensitive UI when the user steps away.
powerMonitor.on('lock-screen', () => blurSensitiveData());Preventing Sleep
Use powerSaveBlocker to keep the system awake during downloads or media playback, then release it promptly.
const { powerSaveBlocker } = require('electron');
const id = powerSaveBlocker.start('prevent-app-suspension');Releasing Blockers
Always call powerSaveBlocker.stop(id) when the task ends, or you will drain the user's battery.
Notifying the Renderer
Forward power events to the UI over IPC so the renderer can update its status indicators.
Designing for Power Awareness
Combine these signals to build apps that are respectful of resources: defer heavy work on battery, resume cleanly after sleep.
Quick Check
Test your power monitoring knowledge.
Recap
You learned to use powerMonitor for suspend/resume, battery and AC detection, idle time, and screen lock events, plus powerSaveBlocker to keep the system awake responsibly.
よくある質問
「電源とハードウェアの監視」レッスンは無料ですか?
はい。「電源とハードウェアの監視」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Electron Desktop App Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
「電源とハードウェアの監視」で何を学びますか?
ElectronのpowerMonitorモジュールでネイティブな電源・ハードウェアイベントを利用し、応答性が高くバッテリーを意識したデスクトップアプリを構築します。 ブラウザで直接実行するハンズオンコードでElectron Desktop App Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Electron Desktop App Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのElectron Desktop App Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「電源とハードウェアの監視」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このElectron Desktop App Developmentレッスンでコードを書いて実行できますか?
はい。すべてのElectron Desktop App Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ネイティブモジュールの利用
- トレイアプリケーションとバッジ
- 画面キャプチャとメディア
- 電源とハードウェアの監視