I want Broadcastreceiver to be called every time Intent.ACTION_BATTERY_CHANGED gets broacasted, so I can track some data.
This broadcast can be sent a lot. Consequently, Android only allows you to run applications to receive this broadcast, so you do not need to fork a bunch of processes, just to say a bunch of applications "hey, the battery level has changed."
This works fine until I use the "Task Manager" of the latest applications to remove one of my preference screens or my main action. Then BroadcastReceiver will no longer be called.
This act completes your process.
So, how can I get a Broadcastreceiver outside of any Activity or Service that can receive broadcasts as long as the user wants?
Usually you register to broadcast in the manifest. For ACTION_BATTERY_CHANGED this is not possible.
Remember that you can poll ACTION_BATTERY_CHANGED - instead of calling registerReceiver() with the actual BroadcastReceiver , go to null . The return value of registerReceiver() will be the last ACTION_BATTERY_CHANGED Intent . So, if you can walk away with a battery level detection every few minutes, use the AlarmManager to wake up so often and check the current battery level.
source share