<?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. HDU 1882 Strange Billboard(位运算)

    题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第 ...

  2. 查杀linux线程指令

      工作中重启环境时常常出现内存溢出等等问题,往往需要查杀进程来帮助重启成功,下面就查杀线程的详细指令做下总结:   1.查找需要kill掉的线程: ps -elf|grep [线程关键信息] 比如: ...

  3. hdu1020 Encoding

    http://acm.hdu.edu.cn/showproblem.php?pid=1020 过了的就是好孩子........ #include<stdio.h> #include< ...

  4. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...

  5. 毕向东JAVA视频讲解(四五课)

    内存的划分: 1,寄存器. 2,本地方法区. 3,方法区. 4,栈内存. 存储的都是局部变量. 而且变量所属的作用域一旦结束,该变量就自动释放. 5,堆内存. 存储是数组和对象(其实数组就是对象) 凡 ...

  6. SDK 与MFC

    SDK 就是Software Development Kit 软件开发包MFC 就是Microsoft Foundation Classes 微软函数类库.是以C++类的形式封装了Windows的AP ...

  7. Xamarin.Android 入门之:Android的生命周期

    一.前言 活动是Android应用程序的基本构建块,他们可以在许多不同的状态存在.当你把一个Android程序置于后台,过一段时间再打开发现之前的数据还存在. 二.活动状态 下面的图表说明了一个活动可 ...

  8. highcharts 结合phantomjs纯后台生成图片

    highcharts 结合phantomjs纯后台生成图片 highcharts 这个图表展示插件我想大家应该都知道,纯javascript编写,相比那些flash图表插件有很大的优势,至少浏览器不用 ...

  9. 24个有用的PHP类库分享

    目前,PHP是用于Web开发的最流行的脚本语言.你可以在互联网上随手找到关于PHP大量资料,包括文档.教程.工具等等.PHP不仅是一种功能丰富的语言,它还能帮助开发人员轻松地创建更好的网络环境.为了进 ...

  10. Lotus Notes中编程发送邮件(二)

    在编程发送各种类似通知的邮件时,时常会需要发件人显示为某个特定的帐户,比如某个部门的名称或者管理员的名字.另一种需求是,用户收到某封邮件后,回复邮件的地址不同于发件人栏显示的地址.而正常情况下,发送邮 ...