How to keep android application running in sleep mode?
The problem is that my app stops working in sleep mo开发者_如何学编程de. The user wants to take orders in sleep mode via a bluetooth scanner. Is there any class which keeps the phone in sleep mode and in the background it takes orders, or any suggestion to achieve that task or any example app?
Its not recommended to run your application in sleep mode, unless you really need that. That's because this drastically affects batter life.
In your case you need to acquire WakeLock
. You can create new WakeLock
instance using PowerManager.newWakeLock()
.
An example from documentation:
PowerManager pm = (PowerManager)mContext.getSystemService(
Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE,
TAG);
wl.acquire();
// ...
wl.release()
This should be sufficient for your needs.
精彩评论