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.

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.