<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:textSize="25sp"
android:text="电池信息简介" /> <ImageView
android:id="@+id/iv_battery"
android:layout_width="match_parent"
android:layout_height="80dip" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView1"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_temperature" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:textColor="@android:color/white"
android:text="电量来源:"
android:textSize="20sp" /> <TextView
android:id="@+id/textCD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:layout_marginTop="15dip"
android:text="充电中!!"
android:textSize="20sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_level" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="电池剩余容量:"
android:textColor="@android:color/white"
android:textSize="20sp" /> <TextView
android:id="@+id/textRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="充电中!!"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView3"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_status" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="电池状态:"
android:textColor="@android:color/white"
android:textSize="20dip" /> <TextView
android:id="@+id/textZT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="充电中!!"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView4"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_technology" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="电池电压:"
android:textColor="@android:color/white"
android:textSize="20dip" /> <TextView
android:id="@+id/textDY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:textColor="@android:color/white"
android:text="充电中!!"
android:textSize="20sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView5"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_plugged" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="电池温度:"
android:textColor="@android:color/white"
android:textSize="20dip" /> <TextView
android:id="@+id/textWD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="充电中!!"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout> <View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView5"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginLeft="10dip"
android:background="@drawable/battery_scale_2" /> <TextView
android:layout_width="140dip"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="电池类型:"
android:textColor="@android:color/white"
android:textSize="20dip" /> <TextView
android:id="@+id/textLX"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="充电中!!"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout> </LinearLayout>
 package com.example.testdemo;

 import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity { private ImageView iv_battery;
private ImageView image; // 电池状态图片
private TextView textCD; // 电池充电状态
private TextView textRL; // 电池剩余容量
private TextView textZT; // 电池状态
private TextView textDY; // 电池电压mV
private TextView textWD; // 电池温度
private TextView textLX; // 电池类型 private BroadcastReceiver myBroadcastReciver = new BroadcastReceiver() { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 电池剩余容量
int level = (int) (intent
.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
/ (float) intent.getIntExtra(BatteryManager.EXTRA_SCALE,
100) * 100);
textRL.setText(level + "%");
// 根据电量判断用什么图片
if (level > 45 || level <= 50) {
iv_battery.setImageResource(R.drawable.battery9);
} else if (level == 0) {
iv_battery.setImageResource(R.drawable.battery0);
} else if (level > 0 || level <= 10) {
iv_battery.setImageResource(R.drawable.battery2);
} else if (level > 10 || level <= 15) {
iv_battery.setImageResource(R.drawable.battery3);
} else if (level > 15 || level <= 20) {
iv_battery.setImageResource(R.drawable.battery4);
} else if (level > 20 || level <= 25) {
iv_battery.setImageResource(R.drawable.battery5);
} else if (level > 25 || level <= 30) {
iv_battery.setImageResource(R.drawable.battery6);
} else if (level > 30 || level <= 35) {
iv_battery.setImageResource(R.drawable.battery7);
} else if (level > 35 || level <= 45) {
iv_battery.setImageResource(R.drawable.battery8);
} else if (level > 50 || level <= 60) {
iv_battery.setImageResource(R.drawable.battery10);
} else if (level > 60 || level <= 65) {
iv_battery.setImageResource(R.drawable.battery11);
} else if (level > 65 || level <= 70) {
iv_battery.setImageResource(R.drawable.battery12);
} else if (level > 70 || level <= 75) {
iv_battery.setImageResource(R.drawable.battery13);
} else if (level > 75 || level <= 80) {
iv_battery.setImageResource(R.drawable.battery14);
} else if (level > 80 || level <= 85) {
iv_battery.setImageResource(R.drawable.battery15);
} else if (level > 85 || level <= 90) {
iv_battery.setImageResource(R.drawable.battery16);
} else if (level > 90 || level <= 100) {
iv_battery.setImageResource(R.drawable.battery17);
}
// 电池当前使用状态
// image.setImageResource(intent.getIntExtra(
// BatteryManager.EXTRA_ICON_SMALL, 0));
switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, 1)) {
case BatteryManager.BATTERY_STATUS_CHARGING:
if (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 1) == BatteryManager.BATTERY_PLUGGED_AC)
textCD.setText("充电器充电中");
else
textCD.setText("USB充电中");
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
textCD.setText("放电中");
break;
case BatteryManager.BATTERY_STATUS_FULL:
textCD.setText("已充满");
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
textCD.setText("未充满");
break;
}
// 电池状态
switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 1)) {
case BatteryManager.BATTERY_HEALTH_DEAD:
textZT.setText("电池已损坏!");
textZT.setTextColor(Color.RED);
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
textZT.setText("健康");
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
textZT.setText("电压过高");
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
textZT.setText("温度过高");
break;
case BatteryManager.BATTERY_HEALTH_UNKNOWN:
textZT.setText("未知");
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
textZT.setText("未知故障");
break;
}
// 电池电压
textDY.setText(intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 1)
+ "mV");
// 电池温度
textWD.setText((intent.getIntExtra(
BatteryManager.EXTRA_TEMPERATURE, 1) / 10.0) + "℃");
// 电池类型
textLX.setText(intent
.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)); }
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
textCD = (TextView) findViewById(R.id.textCD);
textRL = (TextView) findViewById(R.id.textRL);
textZT = (TextView) findViewById(R.id.textZT);
textDY = (TextView) findViewById(R.id.textDY);
textWD = (TextView) findViewById(R.id.textWD);
textLX = (TextView) findViewById(R.id.textLX);
image = (ImageView) findViewById(R.id.imageView1);
iv_battery = (ImageView) findViewById(R.id.iv_battery);
registerReceiver(myBroadcastReciver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(myBroadcastReciver);
}
}

完整代码下载:http://pan.baidu.com/s/1qWrznkg

android电池信息简介的更多相关文章

  1. Android电池信息获取

    Android 可以通过BroadcastReceiver来获取电池信息改变的广播(ACTION_BATTERY_CHANGED),从而获取到相关的电池信息. 电池信息,及其对应的相关常数(参考网址: ...

  2. Android 使用adb查看和修改电池信息

    1.获取电池信息 $ adb shell dumpsys battery $ adb shell dumpsys battery Current Battery Service state: AC p ...

  3. Android资源文件简介

    Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, 资源 ...

  4. Android Action Bar简介

    Android Action Bar简介 Design: Action Bar Action Bar是在屏幕顶端的一部分内容,通常在整个app进行中都保持存在. 它提供了几个关键的功能: 1.使得重要 ...

  5. android电池管理系统从上层的java到底层驱动的调用(转载)

    1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统对电源管理的合理与否直接影响到电池的续航能力,而电池系统作为其中的一部分,主要用于对电池状 ...

  6. 【转】android电池(五):电池 充电IC(PM2301)驱动分析篇

    关键词:android 电池  电量计  PL2301任务初始化宏 power_supply 中断线程化 平台信息:内核:linux2.6/linux3.0系统:android/android4.0  ...

  7. 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇

    关键词:android 电池  电量计  MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...

  8. 【转】android 电池(三):android电池系统

    关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

  9. 【转】android 电池(二):android关机充电流程、充电画面显示

    关键词:android 电池关机充电 androidboot.mode charger关机充电 充电画面显示 平台信息:内核:linux2.6/linux3.0系统:android/android4. ...

随机推荐

  1. java基础知识回顾之---java StringBuilder与StringBuffer异同点

    /*         * StringBuilder与StringBuffer异同点:         *          * 使用方法与StringBuffer完全一样         * Str ...

  2. Android 异步加载

    Android 4.0 后 貌似规定了 在主线程中不允许访问网络,在子线程中不允许修改UI. 否则会抛出NetworkOnMainThreadException 异常 解决办法: 采用继承 Async ...

  3. lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素

    题目: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个.   您在真实的面试中是否遇到过这个题? 样例 给出1->1->2->null,返回 1-& ...

  4. PCB板的价格是怎么算出来的?

    Part 1 :影响一块PCB板价格的各种因素 PCB的价格是很多采购者一直很困惑的事情,很多人在线下单时也会疑问这些价格是怎么算出来的,下面我们就一起谈论一下PCB价格的组成因素. 1.PCB所用材 ...

  5. Java API —— Random类

    1.Random类概述         此类用于产生随机数         如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...

  6. Shuffle和排序

    MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...

  7. 如何学习一个新的PHP框架

    如今的PHP框架层出不穷,我不是这方面的专家,甚至不能熟练地使用其中的一种,所以我不做推荐,也不想讨论哪些算是框架哪些不算框架.这里我要讨论的是如何才能更快地开始使用某个新的框架. 首先你当然必须选择 ...

  8. Java中静态代码块,代码块,构造方法优先级、区别及代码示例

    在项目中遇到了代码块的知识点,跑了下测试,写下结论 代码优先级:静态代码块 -> 构造代码块 -> 构造方法 多个代码块优先级,按照“先定义的代码先执行,后定义的代码后执行”原则执行 静态 ...

  9. EF之MSSQL分布式部署一:EFContext自定义数据库链接

    不废话,上代码: 来源:http://bbs.csdn.net/topics/390823046 原文地址:EF之MSSQL分布式部署一:EFContext自定义数据库链接   /// <sum ...

  10. UVa 11021 (概率 递推) Tribles

    Tribble是麻球? 因为事件都是互相独立的,所以只考虑一只麻球. 设f(i)表示一只麻球i天后它以及后代全部死亡的概率,根据全概率公式: f(i) = P0 + P1 * f(i-1) + P2 ...