I'm trying to make a notification which displays some information about the battery status and refreshes itself every time the BatteryState changes.
First I registered a service in the Manifest.xml:
<service android:name=".BatteryStatusNotification"/>
Then I made the Class and implemented the methods for the service. After that I wrote the NotificationBuilder and the BatteryStateChange BroadcastReceiver.
public class BatteryStatusNotification extends Service {
int level;
String pluggedString;
String temperature;
String voltage;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
DecimalFormat tempformatter = new DecimalFormat("##,##,#");
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
temperature = tempformatter.format(temp);
DecimalFormat volformatter = new DecimalFormat("#,###");
int vol = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
voltage = volformatter.format(vol);
switch (plugged) {
case 0: pluggedString = (getResources().getString(R.string.batteryUncharging));
break;
case 1: pluggedString = (getResources().getString(R.string.batteryChargingAC));
break;
case 2: pluggedString = (getResources().getString(R.string.batteryChargingUSB));
break;
case 4: pluggedString = (getResources().getString(R.string.batteryChargingWireless));
break;
}
batteryStatNotification();
}
};
@Override
public int onStartCommand(Intent intent, int flags, int StartId) {
this.registerReceiver(this.batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return START_STICKY;
}
public void batteryStatNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_battery)
.setContentTitle(pluggedString + " | " + level + " %")
.setContentText(getResources().getString(R.string.notiTemp) + " " + temperature + " | " + getResources().getString(R.string.notiVol) + " " + voltage + getResources().getString(R.string.volts))
.setOngoing(true);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, builder.build()); //Notification Id 2 = Battery
}
@Override
public void onDestroy() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(2);
}
}
In my MainActivity I have a ToggleButton:
//Toggle Battery Notification
ToggleButton batteryToggle = (ToggleButton) findViewById(R.id.batToggleNotification);
batteryToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("batteryToggle", 1);
prefEditor.commit();
enableBatteryNotification();
} else {
enableBatteryNotification();
disableBatteryNotification();
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("batteryToggle", 0);
prefEditor.commit();
}
}
});
int batteryTogglePref = sharedPref.getInt("batteryToggle", 0);
switch (batteryTogglePref) {
case 0: toggle.setChecked(false);
break;
case 1: toggle.setChecked(true);
break;
}
Which saves its own state and returns to it when the App is started and calls these two methods:
public void enableBatteryNotification() {
startService = new Intent(this, BatteryStatusNotification.class);
}
public void disableBatteryNotification() {
stopService(new Intent(getBaseContext(), BatteryStatusNotification.class));
}
I also have an WifiNotification which is refreshed by an alarm. The WifiNotification works perfectly but my BatteryNotification doesn't.
EDIT FOR @Sreekumar R:
public void enableWifiNotification() {
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
int numberSP3 = sharedPref.getInt("numberSP2", 1);
int unitSP3 = sharedPref.getInt("unitSP2", 1);
startService = new Intent(this, WifiNotificationService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, startService, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * numberSP3 * unitSP3 , pendingIntent);
}
Can you explain why I don't need to call:
getBaseContext().startService(startService);
here?
You also need to call
getBaseContext().startService(startService);
or change the line startService = new Intent(this, BatteryStatusNotification.class); to
startService(new Intent(this, BatteryStatusNotification.class));
to start the service.