0

Please note: I am running this whole code in a backgroundService.

All I want is to show a toast message (only once each action) whenever I connect to USB or AC Adapter in my phone,

but the problem comes when toast message if repeated automatically all the time. As BroadcastReceiver is already providing updates constantly the method executes every time and with that toast is repeating again and again. For e.g. if cable is disconnected, either it is USB or AC Adapter, and in its disconnected "Charging Removed" toast message is repeated, even when charger is connected either it AC Adapter or USB the respective message.

import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import static com.hashware.batteryalert.App.channelID;

public class MyService extends Service {

    private static final String TAG = MyService.class.getSimpleName();
    public int level, voltage, status, plugged;

    private final IBinder iBinder = new bindingService();
    private MediaPlayer player;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        IntentFilter intF = new IntentFilter();
        intF.addAction(Intent.ACTION_BATTERY_CHANGED);

        registerReceiver(br,intF);
        Log.d(TAG, "onStartCommand: ServiceStarted");

        return START_STICKY;
    }

//    TODO: Fetching live battery data

    BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
            status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
            plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);

            switch (plugged){
                case BatteryManager.BATTERY_PLUGGED_AC:
                    notificationCreate();
                    Toast.makeText(MyService.this,"Charging : AC Supply", Toast.LENGTH_SHORT).show();
                    break;

                case BatteryManager.BATTERY_PLUGGED_USB:
                    notificationCreate();
                    Toast.makeText(MyService.this,"Charging : USB Supply", Toast.LENGTH_SHORT).show();
                    break;

                case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                    notificationCreate();
                    Toast.makeText(MyService.this,"Charging : Wireless Supply", Toast.LENGTH_SHORT).show();
                    break;

                case 0:
                    stopForeground(true);
                    break;
            }

//            TODO: Notify Battery Status

            if (status == BatteryManager.BATTERY_STATUS_FULL) {
                if (player == null) {
                    player = MediaPlayer.create(MyService.this, R.raw.electric_shock01);
                    player.start();
                    player.setLooping(true);
                }
            } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
                if (player != null) {
                    player.release();
                    player = null;
                    Toast.makeText(MyService.this, "Disconnected After Full charge !", Toast.LENGTH_SHORT).show();
                }
                else if (player == null){
                    Toast.makeText(MyService.this, "Charging Removed !", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onReceive: BLANK");
                }

            }
        }
    };
Community
  • 1
  • 1

1 Answers1

0

If you just need to detect when the phone is connected or disconnected from USB/Adapter, you should be probably Monitoring changes in charging state instead of ACTION_BATTERY_CHANGED. Please refer this https://developer.android.com/training/monitoring-device-state/battery-monitoring#MonitorChargeState

Antonio
  • 1,264
  • 7
  • 12
  • Brother it's really nice of you to provide me your precious support but you're not really clear about the problem I am facing. Problem is: I don't want to repeat those toast messages again and again which is repeated by the BroadcastReceiver. For e.g. even though if I disconnect the charging cable from the phone once, later it will keep on repeated the toast message "Charging Removed", but I want it to toast the message only once whenever I disconnect the charging cable. Same thing happens with the state of charging connected. – Jaidev Pal May 28 '20 at 07:17
  • The problem is you have registered the broadcast receiver for `ACTION_BATTERY_CHANGED` which will receive broadcast whenever the battery percentage is changed and not for the charging connected or disconnected. That is why your code is executed every time the battery level changes and toast is shown even if you have removed the charger. So instead of registering `ACTION_BATTERY_CHANGED`, register for the charging state as mentioned here https://developer.android.com/training/monitoring-device-state/battery-monitoring#MonitorChargeState – Antonio May 28 '20 at 07:35