Power Monitor Service
Access the Power Monitor service using dependency injection:Battery Status
Check if the system is running on battery power:The
isOnBatteryPower() method returns true when the system is running on battery power and false when connected to AC power.System Idle State
Monitor how long the system has been idle and determine the current idle state:Get Idle Time
Get Idle State
Idle States
TheSystemIdleStatesEnum provides the following states:
| State | Description |
|---|---|
ACTIVE | User is actively using the system |
IDLE | System has been idle for the specified threshold |
LOCKED | System is locked by the user |
UNKNOWN | Idle state could not be determined |
Understanding the threshold parameter
Understanding the threshold parameter
The
threshold parameter (in seconds) determines when the system is considered idle:- If the system has been idle for less than the threshold:
ACTIVE - If the system has been idle for more than the threshold:
IDLE - If the system is locked:
LOCKED(regardless of threshold)
60- 1 minute (detect short breaks)300- 5 minutes (detect extended inactivity)600- 10 minutes (detect user absence)
Thermal State
Monitor the system’s thermal state to adjust performance:Thermal States
| State | Description |
|---|---|
NOMINAL | System temperature is normal |
FAIR | System is slightly warm |
SERIOUS | System is hot and may throttle performance |
CRITICAL | System is critically hot |
UNKNOWN | Thermal state could not be determined |
Practical Examples
Adaptive Background Processing
Adjust background task frequency based on power state:Smart Resource Management
Combine multiple power monitoring features:Auto-Pause on Lock
Pause sensitive operations when the system is locked:Battery-Aware Sync
Adjust sync behavior based on power source:Periodic Monitoring
Monitor power state periodically using Laravel’s scheduler:Testing
Use thePowerMonitorFake for testing:
Best Practices
- Check Periodically: Monitor power state at regular intervals, not constantly
- Respect Battery: Reduce background activity when on battery power
- Watch Thermal State: Pause intensive tasks when the system is hot
- Detect Idle: Reduce activity when the user is idle or the system is locked
- User Control: Allow users to override power-saving behaviors in settings
Platform Support
Platform-Specific Behavior
Platform-Specific Behavior
macOS
- Full support for all power monitoring features
- Accurate battery status and thermal state detection
- System idle states work reliably
- Full support for battery and idle state detection
- Thermal state support depends on system hardware
- Battery status depends on the desktop environment
- Thermal state may return
UNKNOWNon some systems - Idle state detection varies by window manager
Troubleshooting
Unknown State Returned
If methods returnUNKNOWN states:
- The feature may not be supported on the current platform
- System permissions may be required
- Hardware may not provide the necessary information
- Fall back to default behavior when receiving
UNKNOWN
Inaccurate Idle Time
If idle time seems incorrect:- Idle time is system-wide, not app-specific
- Some activities (like moving the mouse) reset the idle timer
- Media playback may or may not count as activity (platform-dependent)
- Test with different threshold values
Battery Status Always False
IfisOnBatteryPower() always returns false:
- Desktop computers without batteries will always return false
- Some laptops may not report battery status correctly
- The system may be plugged in
- Check system power settings