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");
}
}
}
};