On Android, applying for Wakelock can keep your process going even if your phone goes into sleep mode, it's useful to have a network function in the background, which can keep the operation going.
Method: Join before the operation
PowerManager pm = (powermanager) mcontext.getsystemservice (Context.power_service);
WakeLock = Pm.newwakelock (Powermanager.partial_wake_lock, Constants.tag);
Wakelock.acquire ();
Where Newwakelock has two parameters, the first parameter defines the behavior, the second parameter is the debug tag, which can be defined as the class name. The first parameter takes the following values:
flag Value |
CPU |
screen |
keyboard |
partial_wake_lock |
on* |
off |
off |
screen_dim_wake_lock |
on |
dim |
off |
screen_bright_wake_lock |
on |
bright |
off |
full_wake_lock |
on |
bright |
bright |
Don't forget to release it after the operation is complete.
if (wakeLock! = null) {
Wakelock.release ();
WakeLock = null;
}
Also, to add permission: <uses-permission android:name= "Android.permission.WAKE_LOCK"/>
--------------------------------------------------------------------------------------------------------------- -------------
Another way is to
- @Override
- protected void onCreate (Bundle icicle) {
- Super . onCreate (Icicle);
- After testing this is still relatively useful, multimedia playback settings are not standby.
- GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- }
This parameter also keeps the screen active and does not go into hibernation, saving some more than the previous security.