การตรวจสอบพลังงานและฮาร์ดแวร์
เชื่อมต่อกับเหตุการณ์พลังงานและฮาร์ดแวร์แบบเนทีฟด้วยโมดูล powerMonitor ของ Electron เพื่อสร้างแอปเดสก์ท็อปที่ตอบสนองได้ดีและคำนึงถึงแบตเตอรี่
การตรวจสอบพลังงานและฮาร์ดแวร์ เป็นบทเรียน Electron Desktop App Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ JavaScript ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 47
คำถามที่พบบ่อย
บทเรียน “การตรวจสอบพลังงานและฮาร์ดแวร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การตรวจสอบพลังงานและฮาร์ดแวร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Electron Desktop App Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบพลังงานและฮาร์ดแวร์”
เชื่อมต่อกับเหตุการณ์พลังงานและฮาร์ดแวร์แบบเนทีฟด้วยโมดูล powerMonitor ของ Electron เพื่อสร้างแอปเดสก์ท็อปที่ตอบสนองได้ดีและคำนึงถึงแบตเตอรี่ คุณปฏิบัติ Electron Desktop App Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Electron Desktop App Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Electron Desktop App Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การตรวจสอบพลังงานและฮาร์ดแวร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Electron Desktop App Development นี้ได้ไหม
ได้ บทเรียน Electron Desktop App Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทำงานกับโมดูลเนทีฟ
- แอปพลิเคชันถาดระบบและป้ายแจ้งเตือน
- การจับภาพหน้าจอและสื่อ
- การตรวจสอบพลังงานและฮาร์ดแวร์