读取 Android 设备的电池信息

分享到:
通过创建BroadcastReceiver来侦测系统中有关电池Intent(ACTION_BATTERY_CHANGED)的变化,一旦有接收到相关事件,将会读取当前电量情况,并通过TextViews显示在当前屏幕。
public class Main extends Activity {
private TextView contentTxt;
private BroadcastReceiver mBatInfoReceiver = newBroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
contentTxt.setText(String.valueOf(level) + "%");
}
};
@Override
public void onCreate(Bundle) {
super.onCreate(icicle);
setContentView(R.layout.main);
contentTxt = (TextView) this.findViewById(R.id.monospaceTxt);
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
昵    称:
验证码:

相关文档: