I have implemented an Android service (START_STICKY
) which starts on device boot and runs in background. Functionality of this service is to interact with SD card. As it is running continuously, started with sticky it consumes battery. To solve heavy battery consumption I wanted to start this service when user is using device.
Ideally start/stop service based on the ACTION_SCREEN_ON
& ACTION_SCREEN_OFF
intents.
When I have tested this found that I can't register for ACTION_SCREEN_OFF
& ACTION_SCREEN_ON
in the Manifest, so I have created a Broadcast Receiver in my service to capture the ACTION_SCREEN_OFF
& ACTION_SCREEN_ON
.
But, since I cannot register for the intents in the manifest, when I stop my service on ACTION_SCREEN_OFF
. How can I possibly start it when the screen comes back on?
Note :
As I have already mentioned SCREEN_ON
+ SCREEN_OFF
cannot be registered in manifest file. It is registered like
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
broadcastReceiver = new TestReceiver();
registerReceiver(broadcastReceiver, filter);
So when service is not running then this intent will not fire.