user2275272
user2275272

Reputation: 11

How to listen to Android's battery warning

I'm developing a game, and it's important that the sudden battery warning doesn't obstruct you. The activity already pauses the game during onPause(), but this isn't called when the battery warning comes up because it's a pop-up on top of the activity, and the activity continues to run.

I don't mean to dismiss the messsage, I just want the activity to know that it should pause the game if it appears, so that you can take your time to read it and dismiss it and not lose because of that.

I have searched a lot and haven't found a question similar to this. Is it possible to detect such a thing?

Upvotes: 1

Views: 115

Answers (2)

VJ Vélan Solutions
VJ Vélan Solutions

Reputation: 6554

You should register for the event broadcast with the System and implement a Broadcast Receiver component.

In your manifest file, you register for receiving the system broadcast pertaining to low batter condition. The relevant intent filter is ACTION_BATTERY_LOW. For a list of other filters related to battery status please refer to Intents and Intent Filters page and search for word BATTERY. HTH.

Upvotes: 1

pinxue
pinxue

Reputation: 1746

You may observe ACTION_BATTERY_LOW broadcast. http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_LOW

Something like:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // where arg1.action is ACTION_BATTERY_LOW

    }

}

Upvotes: 3

Related Questions