How to make android start my application automatically when battery is fully charged?

93 views Asked by At

My question is similar to this one: battery receiver fully charged doesnt work . That question has no answers.

Here are the approaches I have thought of so far:

Approach 1: Register a BroadcastReceiver in the AndroidManifest.xml for ACTION_BATTERY_CHANGED.

This won't work because of the following reason:

I cannot register a BroadcastReceiver in the AndroidManifest.xml to listen for ACTION_BATTERY_CHANGED because it is a Sticky Intent as stated here: https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED.

Approach 2: Register a BroadcastReceiver programatically in the MainActivity.java's onCreate() method.

Issue with this approach is : Avoid registering duplicate broadcast receivers in Android

In addition to the above issue, I do not understand the scope of broadcast receivers properly. To be more precise, I don't understand what happens to the broadcast receivers that an app has registered when the app is:

1) Destroyed by the Android system by calling it's onDestroy() method

2) When the app is force stopped by the user via the Application Manager setting.

Could someone please explain it please?

Approach 3: Use a work around that uses SharedPreferences and a BroadcastReceiver for listening to device reboot.

Have a shared preference that let's me know if I have previously registered a broadcast receiver for ACTION_BATTERY_CHANGED. If yes, don't create a new one, else, create a new broadcast receiver for ACTION_BATTERY_CHANGED. As pointed out in the link in Approach 2, the value in the SharedPreference becomes invalid upon device reboot, so have a separate broadcast receiver to create the receiver again on reboot and update the shared preferences to reflect the same.

Although, approach 3 may work, I feel it is a cumbersome way. Is there a simpler approach?

Additionally, my application will be inefficient since it is going to run for every change in battery state. Is there a way to make it run, only when the battery is full, which is my main purpose?

0

There are 0 answers