这是当我点击UI上的按钮时执行的功能&它显示TextView上的字符串str(充电状态)。如何检查电池何时充满电?
public void checkStatus (View view) {
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Context context = this;
Intent batteryStatus = context.registerReceiver(null, ifilter);
// Are we charging/charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int perc = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
String str;
if (perc == 100) {
str = "Fully charged";
} else if (isCharging) {
str = "Charging";
} else {
str = "Not Charging";
}
TextView textview = (TextView) findViewById(R.id.textView);
textview.setText(str);
}
现在我想要做的是定期检查电池&一旦它是100%(完全充电),然后调用一个函数,让我们叫它DiscntCharger()
从我的研究,我需要使用IntentService来监控充电状态&一旦发现电池已充满电,它需要在我的主要活动中调用该功能。不过,由于我对android开发相当陌生,因此我无法理解我如何实现这一点。
我真的很感激,如果有人能指出我在正确的方向。