Android系列实例之:手机电池还剩多少
2011-11-23 09:46
676 查看
程序将通过注册BroadcastReceiver时设置的IntentFilter来捕捉系统发出的Intent.ACTION_BATTERY_CHANGED这个Action,再以此取得手机电池的计量结果
status(int类型)状态,定义值是BatteryManager.BATTERY_STATUS_XXX。
health(int类型)健康,定义值是BatteryManager.BATTERY_HEALTH_XXX。
present(boolean类型)
level(int类型)电池剩余容量
scale(int类型)电池最大值。通常为100。
icon-small(int类型)图标ID。
temperature(int类型)温度,0.1度单位。
例如表示197的时候,意思为19.7度。
technology(String类型)电池类型
例如,Li-ion等等。
Activity
main.xml
运行效果:
status(int类型)状态,定义值是BatteryManager.BATTERY_STATUS_XXX。
health(int类型)健康,定义值是BatteryManager.BATTERY_HEALTH_XXX。
present(boolean类型)
level(int类型)电池剩余容量
scale(int类型)电池最大值。通常为100。
icon-small(int类型)图标ID。
temperature(int类型)温度,0.1度单位。
例如表示197的时候,意思为19.7度。
technology(String类型)电池类型
例如,Li-ion等等。
Activity
package org.newboy.bat; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button buttonBattery; // 按钮 private BroadcastReceiver mBatReceiver = new BroadcastReceiver() { int level; // 电池还剩多少 int scale; // 总容量 @Override public void onReceive(Context context, Intent intent) { // 得到相应的action String action = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { // 电池剩余容量 level = intent.getIntExtra("level", 0); // 电池最大值 scale = intent.getIntExtra("scale", 100); // 显示电池信息 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("电池信息").setMessage("电池还剩" + String.valueOf(level * 100 / scale) + "%"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点按钮取消广播的接收 unregisterReceiver(mBatReceiver); } }); builder.show(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonBattery = (Button) findViewById(R.id.buttonBattery); buttonBattery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 通过代码注册广播 registerReceiver(mBatReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:layout_margin="2dip" /> <Button android:text="显示电量" android:id="@+id/buttonBattery" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_margin="2dip"></Button> </LinearLayout>
运行效果:
相关文章推荐
- Android深入浅出系列之实例应用—手机页面之间的跳转
- Android深入浅出系列之实例应用—手机页面之间的跳转
- Android深入浅出系列之实例应用—手机页面之间的跳转
- Android获取手机电池电量用法实例
- Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用
- 一个Android应用程序App中存在多少个Context实例对象呢
- 获取android手机电池的电压和温度;
- Cocos2dx游戏开发系列笔记9:android手机上运行《战神传说》,并解决横竖屏即分辨率自适应问题
- 【Android开发—智能家居系列】(三):手机连接WIFI模块
- Android ContentProvider查看/读取手机联系人实例
- Android中判断手机是否联网实例
- Android手机间使用socket进行文件互传实例
- Android手机新手必读系列教程
- Android 系列 2.5监控Android设备的电池电量
- android之检测手机电池
- Android深入浅出系列之实例应用—如何在一个Activity里调用另外一个Activity?
- Android深入浅出系列之实例应用—具有自动提示的菜单应用AutoCompleteTextView和ArrayAdapter的使用
- Android 窗口添加机制系列3-代码实例
- Android深入浅出系列之实例应用—程序运行进度提示条应用ProgressBar和Handler的使用
- 【Android开发—智能家居系列】(三):手机连接WIFI模块