這是當我點擊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開發相當陌生,因此我無法理解我如何實現這一點。
我真的很感激,如果有人能指出我在正確的方向。