<?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. 学习笔记--Grunt、安装、图文详解

    学习笔记--Git安装.图文详解 安装Git成功后,现在安装Gruntjs,官网:http://gruntjs.com/ 一.安装node 参考node.js 安装.图文详解 (最新的node会自动安 ...

  2. Java Socket 使用BufferedWriter和BufferedReader要注意readLine 以及换行标志的发送

    当接收的类使用的是BufferedReader,发送的类是BufferedWriter的时候,要注意发送的一行要有换行标识符. 请看下面一个例子,服务器接收不到客户端的信息. 服务器: import ...

  3. C语言文件操作函数

    C语言文件操作函数大全 clearerr(清除文件流的错误旗标) 相关函数 feof表头文件 #include<stdio.h> 定义函数 void clearerr(FILE * str ...

  4. 2014图灵技术图书最受欢迎TOP15

    来自:图灵社区昨晚给我发的邮件,感觉不错,和大家分享,mark下. [小编语] 回首2014,感谢小伙伴们一路相随.让我们2015一起更快乐地玩耍.今天小编为大家盘点一下过去2014年表现最给力的技术 ...

  5. C#获取一个文件的扩展名

    C#获取一个文件的扩展名System.IO.Path.GetExtension( "文件名 ");ChangeExtension   更改路径字符串的扩展名. Combine   ...

  6. Windows JAVA 环境配置

    Java SE Development Kit Downloads http://www.oracle.com/technetwork/java/javase/overview/index.html ...

  7. [Codeforces673A]Bear and Game(水题,思路)

    题目链接:http://codeforces.com/contest/673/problem/A 题意:一个人看一个90分钟的节目,然后告诉你一些有趣的时刻.这个人假如在15分钟内还没有看到有趣的时刻 ...

  8. 分解成3NF保持函数依赖且为无损连接的算法

    分解成3NF保持函数依赖且为无损连接的算法: 1.根据分解成3NF的保持函数依赖的分解算法(http://www.cnblogs.com/bewolf/p/4443919.html),得到分解结果ρ ...

  9. git文件未改动pull的时候提示冲突

    今天在mac下使用git工具,出现一个很奇怪的问题. 先声明当前工作目录是干净的,运行 git status 没有任何文件改动,且没有任何需要push的文件. 我执行 git pull 命令,直接提示 ...

  10. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...