Hi, I am a beginner. I am trying to get a audio notification when the Android battery is full and low.

I understand the logic. As in, check battery status, then if battery_full run Android Media Player. I am trying to use it for my home needs. This is the MainActivity.java I have

package me.adminweb.batterydost;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.BatteryManager;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Are we charging / charged?

        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

       // How are we charging?
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        if(status == BatteryManager.BATTERY_STATUS_FULL){

            //Play sound using Android MediaPlayer

            MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.batteryfull);
            mediaPlayer.start(); // no need to call prepare(); create() does that for you
        }

Looks like I am missing a few things. I know @Dani through another forum. So thought of seeking help here. Would be nice if you can help me build this app. Thanks.

Dani AI

Generated

Short, practical guidance to finish what started and what pointed toward.

The missing piece in the posted MainActivity is acquiring battery events. Android exposes battery state via system broadcasts: use the sticky ACTION_BATTERY_CHANGED intent to query the current state, or receive ACTION_BATTERY_LOW / ACTION_BATTERY_OKAY to be notified when the system declares low/full. Playing audio directly from a BroadcastReceiver can work, but is fragile: audio focus, Do Not Disturb, and background execution rules make notifications with sound a more robust choice. Also avoid leaking media resources — release any player after playback and prefer short sounds via Notification channels or SoundPool for beeps.

A compact, reliable approach:

  • Register a receiver (manifest or dynamic) for the battery events you want.
  • Create a NotificationChannel (Android O+) and attach a custom sound to that channel so the alert will fire even if your app is not foregrounded.
  • In the receiver post a notification rather than trying to manage long-lived playback from the receiver. If you must play audio directly, acquire audio focus, play then release, and consider using a foreground service if background playback is required.

Example manifest and notification skeleton (adapt and complete in your app):

<receiver android:name=".BatteryReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BATTERY_LOW" />
    <action android:name="android.intent.action.BATTERY_OKAY" />
  </intent-filter>
</receiver>
NotificationCompat.Builder b = new NotificationCompat.Builder(ctx, CHANNEL_ID)
  .setSmallIcon(R.drawable.ic_battery)
  .setContentTitle("Battery alert")
  .setContentText("Battery low")
  .setSound(customUri)
  .setAutoCancel(true);
notificationManager.notify(id, b.build());

Read the official Android guides for the full details: Battery monitoring, Broadcasts, and Building notifications.

Recommended Answers

All 2 Replies

This has been accomplished thousands of times but it's more than 34 lines long so let's find a current code source for 2023.
Read Android-Battery-Alarm
Full source, ready for you to examine and modify.

Thanks, got it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.