android电池信息简介

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="@color/background"
  6     android:gravity="center_horizontal"
  7     android:orientation="vertical" >
  8 
  9     <TextView
 10         android:layout_width="match_parent"
 11         android:layout_height="wrap_content"
 12         android:gravity="center_horizontal|center_vertical"
 13         android:textSize="25sp"
 14         android:text="电池信息简介" />
 15 
 16     <ImageView
 17         android:id="@+id/iv_battery"
 18         android:layout_width="match_parent"
 19         android:layout_height="80dip" />
 20 
 21     <LinearLayout
 22         android:layout_width="match_parent"
 23         android:layout_height="wrap_content"
 24         android:orientation="horizontal" >
 25 
 26         <ImageView
 27             android:id="@+id/imageView1"
 28             android:layout_width="50dip"
 29             android:layout_height="50dip"
 30             android:layout_marginLeft="10dip"
 31             android:background="@drawable/battery_temperature" />
 32 
 33         <TextView
 34             android:layout_width="140dip"
 35             android:layout_height="wrap_content"
 36             android:layout_marginTop="15dip"
 37             android:textColor="@android:color/white"
 38             android:text="电量来源:"
 39             android:textSize="20sp" />
 40 
 41         <TextView
 42             android:id="@+id/textCD"
 43             android:layout_width="match_parent"
 44             android:layout_height="wrap_content"
 45             android:textColor="@android:color/white"
 46             android:layout_marginTop="15dip"
 47             android:text="充电中!!"
 48             android:textSize="20sp" />
 49     </LinearLayout>
 50 
 51     <View
 52         android:layout_width="match_parent"
 53         android:layout_height="1dip"
 54         android:background="#FFFFFF" />
 55 
 56     <LinearLayout
 57         android:layout_width="match_parent"
 58         android:layout_height="wrap_content"
 59         android:orientation="horizontal" >
 60 
 61         <ImageView
 62             android:id="@+id/imageView2"
 63             android:layout_width="50dip"
 64             android:layout_height="50dip"
 65             android:layout_marginLeft="10dip"
 66             android:background="@drawable/battery_level" />
 67 
 68         <TextView
 69             android:layout_width="140dip"
 70             android:layout_height="wrap_content"
 71             android:layout_marginTop="15dip"
 72             android:text="电池剩余容量:"
 73             android:textColor="@android:color/white"
 74             android:textSize="20sp" />
 75 
 76         <TextView
 77             android:id="@+id/textRL"
 78             android:layout_width="match_parent"
 79             android:layout_height="wrap_content"
 80             android:layout_marginTop="15dip"
 81             android:text="充电中!!"
 82             android:textColor="@android:color/white"
 83             android:textSize="20sp" />
 84     </LinearLayout>
 85 
 86     <View
 87         android:layout_width="match_parent"
 88         android:layout_height="1dip"
 89         android:background="#FFFFFF" />
 90 
 91     <LinearLayout
 92         android:layout_width="match_parent"
 93         android:layout_height="wrap_content"
 94         android:orientation="horizontal" >
 95 
 96         <ImageView
 97             android:id="@+id/imageView3"
 98             android:layout_width="50dip"
 99             android:layout_height="50dip"
100             android:layout_marginLeft="10dip"
101             android:background="@drawable/battery_status" />
102 
103         <TextView
104             android:layout_width="140dip"
105             android:layout_height="wrap_content"
106             android:layout_marginTop="15dip"
107             android:text="电池状态:"
108             android:textColor="@android:color/white"
109             android:textSize="20dip" />
110 
111         <TextView
112             android:id="@+id/textZT"
113             android:layout_width="match_parent"
114             android:layout_height="wrap_content"
115             android:layout_marginTop="15dip"
116             android:text="充电中!!"
117             android:textColor="@android:color/white"
118             android:textSize="20sp" />
119     </LinearLayout>
120 
121     <View
122         android:layout_width="match_parent"
123         android:layout_height="1dip"
124         android:background="#FFFFFF" />
125 
126     <LinearLayout
127         android:layout_width="match_parent"
128         android:layout_height="wrap_content"
129         android:orientation="horizontal" >
130 
131         <ImageView
132             android:id="@+id/imageView4"
133             android:layout_width="50dip"
134             android:layout_height="50dip"
135             android:layout_marginLeft="10dip"
136             android:background="@drawable/battery_technology" />
137 
138         <TextView
139             android:layout_width="140dip"
140             android:layout_height="wrap_content"
141             android:layout_marginTop="15dip"
142             android:text="电池电压:"
143             android:textColor="@android:color/white"
144             android:textSize="20dip" />
145 
146         <TextView
147             android:id="@+id/textDY"
148             android:layout_width="match_parent"
149             android:layout_height="wrap_content"
150             android:layout_marginTop="15dip"
151             android:textColor="@android:color/white"
152             android:text="充电中!!"
153             android:textSize="20sp" />
154     </LinearLayout>
155 
156     <View
157         android:layout_width="match_parent"
158         android:layout_height="1dip"
159         android:background="#FFFFFF" />
160 
161     <LinearLayout
162         android:layout_width="match_parent"
163         android:layout_height="wrap_content"
164         android:orientation="horizontal" >
165 
166         <ImageView
167             android:id="@+id/imageView5"
168             android:layout_width="50dip"
169             android:layout_height="50dip"
170             android:layout_marginLeft="10dip"
171             android:background="@drawable/battery_plugged" />
172 
173         <TextView
174             android:layout_width="140dip"
175             android:layout_height="wrap_content"
176             android:layout_marginTop="15dip"
177             android:text="电池温度:"
178             android:textColor="@android:color/white"
179             android:textSize="20dip" />
180 
181         <TextView
182             android:id="@+id/textWD"
183             android:layout_width="match_parent"
184             android:layout_height="wrap_content"
185             android:layout_marginTop="15dip"
186             android:text="充电中!!"
187             android:textColor="@android:color/white"
188             android:textSize="20sp" />
189     </LinearLayout>
190 
191     <View
192         android:layout_width="match_parent"
193         android:layout_height="1dip"
194         android:background="#FFFFFF" />
195 
196     <LinearLayout
197         android:layout_width="match_parent"
198         android:layout_height="wrap_content"
199         android:orientation="horizontal" >
200 
201         <ImageView
202             android:id="@+id/imageView5"
203             android:layout_width="50dip"
204             android:layout_height="50dip"
205             android:layout_marginLeft="10dip"
206             android:background="@drawable/battery_scale_2" />
207 
208         <TextView
209             android:layout_width="140dip"
210             android:layout_height="wrap_content"
211             android:layout_marginTop="15dip"
212             android:text="电池类型:"
213             android:textColor="@android:color/white"
214             android:textSize="20dip" />
215 
216         <TextView
217             android:id="@+id/textLX"
218             android:layout_width="match_parent"
219             android:layout_height="wrap_content"
220             android:layout_marginTop="15dip"
221             android:text="充电中!!"
222             android:textColor="@android:color/white"
223             android:textSize="20sp" />
224     </LinearLayout>
225 
226 </LinearLayout>
  1 package com.example.testdemo;
  2 
  3 import android.app.Activity;
  4 import android.content.BroadcastReceiver;
  5 import android.content.Context;
  6 import android.content.Intent;
  7 import android.content.IntentFilter;
  8 import android.graphics.Color;
  9 import android.os.BatteryManager;
 10 import android.os.Bundle;
 11 import android.view.Window;
 12 import android.widget.ImageView;
 13 import android.widget.TextView;
 14 
 15 public class MainActivity extends Activity {
 16 
 17     private ImageView iv_battery;
 18     private ImageView image;  // 电池状态图片
 19     private TextView textCD;  // 电池充电状态
 20     private TextView textRL;  // 电池剩余容量
 21     private TextView textZT;  // 电池状态
 22     private TextView textDY;  // 电池电压mV
 23     private TextView textWD;  // 电池温度
 24     private TextView textLX;  // 电池类型
 25     
 26     private BroadcastReceiver myBroadcastReciver = new BroadcastReceiver() {
 27 
 28         @Override
 29         public void onReceive(Context context, Intent intent) {
 30             // TODO Auto-generated method stub
 31             // 电池剩余容量
 32             int level = (int) (intent
 33                     .getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
 34                     / (float) intent.getIntExtra(BatteryManager.EXTRA_SCALE,
 35                             100) * 100);
 36             textRL.setText(level + "%");
 37             // 根据电量判断用什么图片
 38             if (level > 45 || level <= 50) {
 39                 iv_battery.setImageResource(R.drawable.battery9);
 40             } else if (level == 0) {
 41                 iv_battery.setImageResource(R.drawable.battery0);
 42             } else if (level > 0 || level <= 10) {
 43                 iv_battery.setImageResource(R.drawable.battery2);
 44             } else if (level > 10 || level <= 15) {
 45                 iv_battery.setImageResource(R.drawable.battery3);
 46             } else if (level > 15 || level <= 20) {
 47                 iv_battery.setImageResource(R.drawable.battery4);
 48             } else if (level > 20 || level <= 25) {
 49                 iv_battery.setImageResource(R.drawable.battery5);
 50             } else if (level > 25 || level <= 30) {
 51                 iv_battery.setImageResource(R.drawable.battery6);
 52             } else if (level > 30 || level <= 35) {
 53                 iv_battery.setImageResource(R.drawable.battery7);
 54             } else if (level > 35 || level <= 45) {
 55                 iv_battery.setImageResource(R.drawable.battery8);
 56             } else if (level > 50 || level <= 60) {
 57                 iv_battery.setImageResource(R.drawable.battery10);
 58             } else if (level > 60 || level <= 65) {
 59                 iv_battery.setImageResource(R.drawable.battery11);
 60             } else if (level > 65 || level <= 70) {
 61                 iv_battery.setImageResource(R.drawable.battery12);
 62             } else if (level > 70 || level <= 75) {
 63                 iv_battery.setImageResource(R.drawable.battery13);
 64             } else if (level > 75 || level <= 80) {
 65                 iv_battery.setImageResource(R.drawable.battery14);
 66             } else if (level > 80 || level <= 85) {
 67                 iv_battery.setImageResource(R.drawable.battery15);
 68             } else if (level > 85 || level <= 90) {
 69                 iv_battery.setImageResource(R.drawable.battery16);
 70             } else if (level > 90 || level <= 100) {
 71                 iv_battery.setImageResource(R.drawable.battery17);
 72             }
 73             // 电池当前使用状态
 74             // image.setImageResource(intent.getIntExtra(
 75             // BatteryManager.EXTRA_ICON_SMALL, 0));
 76             switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, 1)) {
 77             case BatteryManager.BATTERY_STATUS_CHARGING:
 78                 if (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 1) == BatteryManager.BATTERY_PLUGGED_AC)
 79                     textCD.setText("充电器充电中");
 80                 else
 81                     textCD.setText("USB充电中");
 82                 break;
 83             case BatteryManager.BATTERY_STATUS_DISCHARGING:
 84                 textCD.setText("放电中");
 85                 break;
 86             case BatteryManager.BATTERY_STATUS_FULL:
 87                 textCD.setText("已充满");
 88                 break;
 89             case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
 90                 textCD.setText("未充满");
 91                 break;
 92             }
 93             // 电池状态
 94             switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 1)) {
 95             case BatteryManager.BATTERY_HEALTH_DEAD:
 96                 textZT.setText("电池已损坏!");
 97                 textZT.setTextColor(Color.RED);
 98                 break;
 99             case BatteryManager.BATTERY_HEALTH_GOOD:
100                 textZT.setText("健康");
101                 break;
102             case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
103                 textZT.setText("电压过高");
104                 break;
105             case BatteryManager.BATTERY_HEALTH_OVERHEAT:
106                 textZT.setText("温度过高");
107                 break;
108             case BatteryManager.BATTERY_HEALTH_UNKNOWN:
109                 textZT.setText("未知");
110                 break;
111             case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
112                 textZT.setText("未知故障");
113                 break;
114             }
115             // 电池电压
116             textDY.setText(intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 1)
117                     + "mV");
118             // 电池温度
119             textWD.setText((intent.getIntExtra(
120                     BatteryManager.EXTRA_TEMPERATURE, 1) / 10.0) + "℃");
121             // 电池类型
122             textLX.setText(intent
123                     .getStringExtra(BatteryManager.EXTRA_TECHNOLOGY));
124 
125         }
126     };
127 
128     @Override
129     public void onCreate(Bundle savedInstanceState) {
130         super.onCreate(savedInstanceState);
131         requestWindowFeature(Window.FEATURE_NO_TITLE);
132         setContentView(R.layout.activity_main);
133         textCD = (TextView) findViewById(R.id.textCD);
134         textRL = (TextView) findViewById(R.id.textRL);
135         textZT = (TextView) findViewById(R.id.textZT);
136         textDY = (TextView) findViewById(R.id.textDY);
137         textWD = (TextView) findViewById(R.id.textWD);
138         textLX = (TextView) findViewById(R.id.textLX);
139         image = (ImageView) findViewById(R.id.imageView1);
140         iv_battery = (ImageView) findViewById(R.id.iv_battery);
141         registerReceiver(myBroadcastReciver, new IntentFilter(
142                 Intent.ACTION_BATTERY_CHANGED));
143     }
144 
145     @Override
146     protected void onDestroy() {
147         // TODO Auto-generated method stub
148         super.onDestroy();
149         unregisterReceiver(myBroadcastReciver);
150     }
151 }

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

原文地址:https://www.cnblogs.com/androidsj/p/3997218.html