<?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. javascript中li标签的排序和数组sort的用法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. 学习selenium需要具备的知识或技术

    因为有QTP和JAVA的基础,selenium学起来会快很多,总结下学习selenium所需要具备的知识或技术 1.selenium进行的自动化测试是基于ui层面的,所以html,css,javasc ...

  3. js中的call、apply

    function qingyezhuA(a0, a1) { this.qingyezhuX = a0 + a1; } var qingyezhuObj1 = { }; qingyezhuA.apply ...

  4. windows下安装ubantu

        首先声明我是一个linux大菜鸟,之所以学这个,一个是好玩,另外做DL的一些软件如Caffe要在这个平台上运行,所以没事就鼓捣鼓捣.linux是一种内核,市场上支持这种内核的操作系统有uban ...

  5. 用 React 编写2048游戏

    1.代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="U ...

  6. MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上)

    有A数据库服务器,B本机:    我现在想通过在B机器上通过代码调用SQL来执行A数据库的备份到B机器上    调用的SQL语句为:Backup Database MYDATABASE To Disk ...

  7. Android 时间轴TimeLine

    代码:这里

  8. QTP10&QTP11&UFT11.5的安装和破解

    QTP10的安装和破解方法 下载QTP10.0并安装. 安装成功后,在C:\Program Files\Common Files\Mercury Interactive下创建文件夹:License M ...

  9. c 语言练习__去掉多余的空白字符_修正

    #include <stdio.h> #include <string.h> #include <errno.h> #define BUF_SIZE 128 /* ...

  10. iOS学习笔记: 使用CAShapeLayer创建带有空心区域的遮罩层

    CAShapeLayer是用来接受矢量Path,直接使用GPU来进行渲染的特殊图层.看下面效果: 对应代码: let markLayer = CAShapeLayer(); markLayer.fra ...