I developed a BroadcastReceiver to respond to the MC18 being cradled, but it isn't working as expected. The intent "android.intent.action.ACTION_POWER_CONNECTED" is received, but there are no extras to help me determine if the battery is charging in the way that Google recommends Monitoring the Battery Level and Charging State | Android Developers.
How can I determine if this is specific to the MC18?
PowerConnectionReceiver powerReceiver = new PowerConnectionReceiver();
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
if (isCharging) {
disposable = true;
finish();
}
}
}
thank you.
3 Replies
To get the battery full or battery charging status, we will require to register ACTION_BATTERY_CHANGED which is a sticky intent.
So registering via the AndroidManifest.xml file will not yield the data and notifications.
The links would be helpful
http://developer.android.com/reference/android/content/Intent.html#ACTI…
http://stackoverflow.com/questions/19782993/broadcastreceiver-allways-g…
Following code snippet could be helpful:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batterychnage, filter);
}
private BroadcastReceiver batterychnage = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
if(status == BatteryManager.BATTERY_STATUS_CHARGING)
{
Log.d("App","CHARGING");
}
if(status == BatteryManager.BATTERY_STATUS_FULL)
{
Log.d("App","Battery Full");
}
}
};
Thank you.
Kavya,
Thank you for the recomendation. Unfortunately that does not work at all for me. No intent is ever delivered to my receiver. None of the following code is executed using ACTION_BATTERY_CHANGED
Receiver:
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
Log.v(LOGTAG, "Power Connected");
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
if (status == -1) {
isCharging = true;
}
if (isCharging) {
disposable = true;
finish();
}
}
}
onCreate()
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("android.intent.action.ACTION_BATTERY_CHANGED");
registerReceiver(powerReceiver, iFilter);
Log.v(LOGTAG, "Receiver Registered");
}
Thanks again.
Thank you Kavya.
Attached helper application