10.6 PowerManager(电源服务)
Category Android Basic Tutorial
Introduction:
>
This section will cover PowerManager(电源服务), a system service provided by Android for managing CPU operation, keyboard, or screen brightness. However, unless absolutely necessary, avoid using this class. If you do use it, ensure it is released promptly! This section will not delve too deeply into this topic as it involves low-level details. We will focus on basic concepts, PowerManager, and the mechanism of wakelock locks.
Official API Documentation: PowerManager
1. What is PowerManager?
>
PowerManager is an API provided by the Android system for power management, which significantly impacts device battery life. The official recommendation is to avoid using this class unless absolutely necessary, and to release it immediately after use.
Power management includes: CPU operation, keyboard, or screen brightness. The core mechanism is the wakelock lock. As long as this lock is held, the system cannot enter sleep mode, allowing user-space programs or the kernel to acquire it. Locks can be "timed" or "untimed". Timed locks unlock automatically after their time expires, and if there are no locks or they expire, the kernel initiates sleep mode.
2. Introduction to Wakelock Locks
>
PowerManager.WakeLock has locking and unlocking states, with two locking methods:
① Permanent Lock: This lock remains until explicitly released, so it must be used with caution.
② Timed Lock: This lock releases automatically after a set time. WakeLock has two locking mechanisms: ① Non-counting lock mechanism and ② Counting lock mechanism (default), which can be specified using setReferenceCounted (boolean value). The difference is: The former releases the lock with a single release () regardless of how many times acquire () is called. The latter requires (--count == 0) to release and (count == 0) to acquire.
WakeLock's counting mechanism does not actually request/release a lock for each call; it only counts the number of times the same lock is requested/released and then operates accordingly.
3. How to Use PowerManager
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock w1 = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "MyTag");
w1.acquire();
// During this process, the screen will remain bright!
w1.release();
The first flag in newWakeLock () significantly impacts system power.
These flags are exclusive and can only be specified one at a time.
PARTIALWAKELOCK: Keeps the CPU running, with the screen and keyboard lights possibly off.
SCREENDIMWAKE_LOCK: Keeps the CPU running, allows the screen to display but possibly dim, and allows the keyboard light to be off.
SCREENBRIGHTWAKE_LOCK: Keeps the CPU running, allows the screen to display brightly, and allows the keyboard light to be off.
FULLWAKELOCK: Keeps the CPU running, keeps the screen brightly displayed, and keeps the keyboard light on.
4. Required Permissions
To perform power operations, declare the necessary permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
You may also need:
<uses-permission android:name="android.permission.DEVICE_POWER"/>
WakeLock settings are Activity-level, not application-wide.
Summary:
>
This section introduced PowerManager (电源服务), but it was more of a科普. It's recommended to avoid using this class unless absolutely necessary. Whether you understood it or not, it's okay; just be aware of it. -2.5.0 Building a Reusable Custom BaseAdapter
-2.5.1 Implementing Multiple Layouts for ListView Items
-2.5.2 Basic Usage of GridView (Grid View)
-2.5.3 Basic Usage of Spinner (Dropdown List)
-2.5.4 Basic Usage of AutoCompleteTextView (Auto-complete Text Field)
-2.5.5 Basic Usage of ExpandableListView (Collapsible List)
-2.5.6 Basic Usage of ViewFlipper (Flip View)
-2.5.8 Detailed Explanation of Notification (Status Bar Notification)
-2.5.9 Detailed Explanation of AlertDialog (Dialog Box)
-2.6.0 Basic Usage of Other Common Dialogs
-2.6.1 Basic Usage of PopupWindow (Floating Box)
-2.6.3 Simple Usage of ViewPager
-2.6.4 Simple Usage of DrawerLayout (Official Side Sliding Menu)
-3.1.1 Event Handling Mechanism Based on Listeners
-3.2 Event Handling Mechanism Based on Callbacks
-3.3 Analysis of Handler Message Passing Mechanism
-3.4 TouchListener vs OnTouchEvent + Multi-touch
-3.5 Listening for Content Changes in EditText
-3.6 Responding to System Setting Events (Configuration Class)
-3.7 AsyncTask Asynchronous Task
-4.3.1 BroadcastReceiver Beginner
-4.3.2 BroadcastReceiver Advanced
-4.4.1 ContentProvider Introduction
-4.4.2 ContentProvider Deep Dive — Document Provider
-4.5.2 Passing Complex Data with Intent
-5.2.1 Fragment Example Detailed — Bottom Navigation Bar Implementation (Method 1)
-5.2.2 Fragment Example Detailed — Bottom Navigation Bar Implementation (Method 2)
-5.2.3 Fragment Example Detailed — Bottom Navigation Bar Implementation (Method 3)
-5.2.4 Fragment Example Detailed — Bottom Navigation Bar + ViewPager Page Swipe
-5.2.5 Fragment Example Detailed — Simple Implementation of News (Shopping) App List Fragment
-6.1 Data Storage and Access — File Storage and Reading
-6.2 Data Storage and Access — SharedPreferences for Saving User Preferences
-6.3.1 Data Storage and Access — Introduction to SQLite Database
-6.3.2 Data Storage and Access — Deep Dive into SQLite Database
-7.1.1 Android Network Programming Essentials and HTTP Protocol Study
-7.1.2 Android HTTP Request and Response Headers Study
-7.1.3 Android HTTP Request Method: HttpURLConnection
-7.1.4 Android HTTP Request Method: HttpClient
-7.2.1 Android XML Data Parsing
-7.2.2 Android JSON Data Parsing
-7.3.2 Android File Download (1)
-7.3.3 Android File Download (2)
-7.4 Android Calling WebService
-7.5.1 Basic Usage of WebView (Web View)
-7.5.2 WebView and JavaScript Interaction Basics
-7.5.3 WebView Considerations After Android 4.4
-7.5.6 WebView Handling Webpage Error Codes
-7.6.1 Socket Network Basics Preparation
-7.6.2 TCP Protocol Based Socket Communication (1)
-7.6.3 TCP Protocol Based Socket Communication (2)
-7.6.4 UDP Protocol Based Socket Communication
-8.1.1 Summary of 13 Drawable Types in Android Part 1
-8.1.2 Summary of 13 Drawable Types in Android Part 2
-8.1.3 Summary of 13 Drawable Types in Android Part 3
-8.2.1 Bitmap (Bitmap) Comprehensive Analysis Part 1
-8.3.1 Detailed Explanation of Three Drawing Tool Classes
-8.3.2 Drawing Class Practical Examples
-8.3.3 Paint API — MaskFilter (Mask)
-8.3.4 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 1)
-8.3.5 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 2)
-8.3.6 Paint API — Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API - Xfermode and PorterDuff Explained (Part 4)
- 8.3.8 Paint API - Xfermode and PorterDuff Explained (Part 5)
- 8.3.9 Paint API - ColorFilter (Color Filter) (1/3)
- 8.3.10 Paint API - ColorFilter (Color Filter) (2/3)
- 8.3.11 Paint API - ColorFilter (Color Filter) (3/3)
- 8.3.12 Paint API - PathEffect (Path Effects)
- 8.3.13 Paint API - Shader (Image Rendering)
- 8.3.14 Paint Enumerations/Constants and ShadowLayer Shadow Effects
- 8.3.15 Paint API - Typeface (Font Style)
- 8.3.16 Canvas API Explained (Part 1)
- 8.3.17 Canvas API Explained (Part 2) Clipping Methods
- 8.3.18 Canvas API Explained (Part 3) Matrix and drawBitmapMesh
- 8.4.1 Android Animation Collection - Frame Animation
- 8.4.2 Android Animation Collection - Tween Animation
- 8.4.3 Android Animation Collection - Property Animation - First Look
- 8.4.4 Android Animation Collection - Property Animation - Another Look
- 9.1 Using SoundPool to Play Sound Effects (Duang~)
- 9.2 MediaPlayer to Play Audio and Video
- 9.3 Using Camera to Take Photos
- 9.4 Using MediaRecord to Record Audio
- 10.1 TelephonyManager (Phone Manager)
- 10.2 SmsManager (SMS Manager)
- 10.3 AudioManager (Audio Manager)
- 10.4 Vibrator (Vibrator)
- 10.5 AlarmManager (Alarm Service)
- 10.6 PowerManager (Power Service)
- 10.7 WindowManager (Window Management Service)
- 10.8 LayoutInflater (Layout Service)
- 10.9 WallpaperManager (Wallpaper Manager)
- 10.10 Sensor Topic (1) - Introduction
- 10.11 Sensor Topic (2) - Orientation Sensor
- 10.12 Sensor Topic (3) - Accelerometer/Gyroscope Sensor
- 10.12 Sensor Topic (4) - Understanding Other Sensors
- 10.14 Android GPS Introduction
- 11.0 "2015 Latest Android Basic Tutorial" Completion Celebration
- 12.1 Android Practice: DrySister Look at Girls App (First Edition) - Project Setup and Simple Implementation
- 12.2 DrySister Look at Girls App (First Edition) - 2. Parsing Backend Data
- 12.3 DrySister Look at Girls App (First Edition) - 3. Image Loading Optimization (Writing an Image Cache Framework)
- 12.4 DrySister Look at Girls App (First Edition) - 4. Adding Data Caching (Introducing SQLite)
- 12.5 DrySister Look at Girls App (First Edition) - 5. Code Review, Adjustment, and Logging Class Writing
- 12.6 DrySister Look at Girls App (First Edition) - 6. Icon Creation, Obfuscation, Signing, APK Slimming, App Release
WeChat Subscription
English: