开发者

How to keep android application running in sleep mode?

开发者 https://www.devze.com 2023-03-27 01:04 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消