@Hoan Nguyen 是对的。您的活动可以注册电池事件。这是一个例子:
public class Test extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView text = new TextView(this);
setContentView(text);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean charging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
text.setText(charging ? "Charging" : "On battery");
}
}, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
@lenik 可能也是正确的,您应该在 onResume()/onPause() 而不是 onCreate() 中注册/取消注册,但以上内容应该可以帮助您入门。