android显示手机电量
package com.basillee.asus.demo; import android.app.Notification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity11 extends ActionBarActivity { private TextView textViewLevel=null; private int batteryLevel; private int batteryScale; private Button buttonBatteryShow; private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //获取当前电量,如未获取具体数值,则默认为0 batteryLevel=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0); //获取最大电量,如未获取到具体数值,则默认为100 batteryScale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,100); //显示电量 textViewLevel.setText("电量"+(batteryLevel*100/batteryScale)+"%"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity11); textViewLevel= (TextView) findViewById(R.id.textViewBattery); buttonBatteryShow= (Button) findViewById(R.id.button_show_battery); buttonBatteryShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { IntentFilter intentFilter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED); //注册接收器以获取电量信息 registerReceiver(broadcastReceiver, intentFilter); } }); } }
http://jingyan.baidu.com/season/48891