android 显示电池电量
原文引入:http://blog.csdn.net/it_farmer2010/article/details/6334051
没事的时候,记得多学一点,每天进步一点点,好让自己跟上这个每天在变化的技术环境,话不多说,上代码。
直接上代码吧。
package com.jli.battery; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.BitmapFactory; import android.os.BatteryManager; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; public class Main extends Activity { private ImageView tvBatteryChanged; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvBatteryChanged = (ImageView)findViewById(R.id.test); //注册一个接受广播类型 registerReceiver(new BatteryBroadcastReceiver(), new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } /**接受电量改变广播*/ class BatteryBroadcastReceiver extends BroadcastReceiver{ int intLevel = 0; int intScale = 0; String BatteryV=null;//电压 String BatteryT=null;//温度 String BatteryStatus=null;//电池状态 String BatteryStatus2=null;//电池充电方式 String BatteryTemp=null; //电池键康状态 @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){ intLevel = intent.getIntExtra("level", 0); intScale = intent.getIntExtra("scale", 100); int curPower = (intLevel * 100 / intScale)/25;// 计算当前电量 switch (curPower) { case 0: tvBatteryChanged.setImageBitmap(BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.power0)); break; case 1: tvBatteryChanged.setImageBitmap(BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.power1)); break; case 2: tvBatteryChanged.setImageBitmap(BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.power2)); break; case 3: tvBatteryChanged.setImageBitmap(BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.power3)); break; case 4: tvBatteryChanged.setImageBitmap(BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.power4)); break; } String action = intent.getAction(); /* * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver() */ if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { Log.d("Battery V", "" + intent.getIntExtra("voltage", 0)); // 电池伏数 Log.d("Battery T", "" + intent.getIntExtra("temperature", 0)); // 电池温度 BatteryV = "当前电压为:" + intent.getIntExtra("voltage", 0); BatteryT = "当前温度为:" + (intent.getIntExtra("temperature", 0)*0.1); switch (intent.getIntExtra("status",BatteryManager.BATTERY_STATUS_UNKNOWN)) { case BatteryManager.BATTERY_STATUS_CHARGING: BatteryStatus = "充电状态"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING: BatteryStatus = "放电状态"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: BatteryStatus = "未充电"; break; case BatteryManager.BATTERY_STATUS_FULL: BatteryStatus = "充满电"; break; case BatteryManager.BATTERY_STATUS_UNKNOWN: BatteryStatus = "未知道状态"; break; } switch (intent.getIntExtra("plugged", BatteryManager.BATTERY_PLUGGED_AC)) { case BatteryManager.BATTERY_PLUGGED_AC: BatteryStatus2 = "AC充电"; break; case BatteryManager.BATTERY_PLUGGED_USB: BatteryStatus2 = "USB充电"; break; } switch (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)) { case BatteryManager.BATTERY_HEALTH_UNKNOWN: BatteryTemp = "未知错误"; break; case BatteryManager.BATTERY_HEALTH_GOOD: BatteryTemp = "状态良好"; break; case BatteryManager.BATTERY_HEALTH_DEAD: BatteryTemp = "电池没有电"; break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: BatteryTemp = "电池电压过高"; break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: BatteryTemp = "电池过热"; break; } } ListView lv = (ListView)findViewById(R.id.batteryListView); String [] batterys={BatteryV,BatteryT,"电池状态:"+BatteryStatus,"电池充电方式:"+BatteryStatus2,"电池键康状态:"+BatteryTemp}; lv.setAdapter(new ArrayAdapter(Main.this, android.R.layout.simple_list_item_1,batterys)); } } } }
然后要在AndroidManifest.xml 申请电池检测权限。
<uses-permission android:name="android.permission.BATTERY_STATS" />
最后看一下效果图吧。
注:最上边的那个ImageView 是展示的一张图片,跟据当前电量检测不同显示 的。