正文  设备功能 > 设备信息 >

读取android设备的电池信息

简短说明(Quick explanation):通过创建BroadcastReceiver来侦测系统中有关电池Intent(ACTION_BATTERY_CHANGED)的变化,一旦有接收到相关事件,将会读取当前电量情况,并通过TextViews显示在当前屏幕。[代码] M......

简短说明(Quick explanation):
通过创建BroadcastReceiver来侦测系统中有关电池Intent(ACTION_BATTERY_CHANGED)的变化,一旦有接收到相关事件,将会读取当前电量情况,并通过TextViews显示在当前屏幕。

[代码] Main.java

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));
   }

}