Skip to content Skip to sidebar Skip to footer

Android Background Service And Wake Lock

I have android background service to connect with my RabbitMQ server. My background service listen incoming rabbitmq message. Everything is working well but the problem is appear w

Solution 1:

You need a Wake Lock to keep the phone running when screen is off. Wake locks allow your application to control the power state of the host device.

Add the WAKE_LOCK permission to your application's manifest file:

<uses-permissionandroid:name="android.permission.WAKE_LOCK" />

Then add the following in onCreate():

PowerManagerpm= (PowerManager) getSystemService(Context.POWER_SERVICE);  
wakelock= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getCanonicalName());
wakelock.acquire();

Solution 2:

If your app includes a broadcast receiver that uses a service to do some work, you can manage your wake lock through a WakefulBroadcastReceiver. This is the preferred approach. If your app doesn't follow that pattern, you set a wake lock directly.

Post a Comment for "Android Background Service And Wake Lock"