Android - 电池状态

为了解决电池图标的问题,顺带看了看电池信息的获取方法 ;自己写了一个小栗子,来验证一下效果

电池的信息,一般都在BatteryManager里面,信息是用广播发出的。我们更新信息需要一个广播接收器

注册一个广播接收器,接收  Intent.ACTION_BATTERY_CHANGED ,从intent中读出想要的电池信息

比如 BatteryManager.EXTRA_STATUS     BatteryManager.BATTERY_STATUS_CHARGING  等等

在Android 5.1中,电池信息可以在Settings - Battery 里面找到

 1 package com.example.chargingwatching;
 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.os.BatteryManager;
 9 import android.os.Bundle;
10 import android.view.WindowManager;
11 import android.widget.TextView;
12
13 public class MainActivity extends Activity {
14     private TextView chargeStatus;
15     private TextView LevelDigit;
16     private TextView percentView;
17     private int rustLevel = 0;    /* battery level */
18     private int windowWidth = 0;
19     private WindowManager mWindowManager;
20
21     private BroadcastReceiver mBatteryStatuReceiver = new BroadcastReceiver() {
22         int status;    // current battery status
23         int plugType;
24         public void onReceive(Context context, Intent intent)    {
25
26             rustLevel = (int)(100f
27                     * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
28                     / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
29
30             LevelDigit.setText("  " + rustLevel + "%");
31
32             percentView.setWidth((int)(windowWidth * rustLevel /100));
33             status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
34                     BatteryManager.BATTERY_STATUS_UNKNOWN);
35             plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
36
37             if(status == BatteryManager.BATTERY_STATUS_CHARGING) {
38                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
39                     chargeStatus.setText(R.string.ac_charging);
40                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
41                     chargeStatus.setText(R.string.usb_charging);
42                 }
43             } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
44                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
45                     chargeStatus.setText(R.string.full_ac);
46                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
47                     chargeStatus.setText(R.string.full_usb);
48                 }
49             } else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
50                 chargeStatus.setText(R.string.uncharge);
51             } else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
52                 chargeStatus.setText(R.string.discharging);
53             } else {
54                 chargeStatus.setText(R.string.unknown);
55             }
56         }
57     };
58
59     @SuppressWarnings("deprecation")
60     @Override
61     public void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         setContentView(R.layout.watch_charging);
64         chargeStatus = (TextView)findViewById(R.id.tv_charge);
65         LevelDigit = (TextView) findViewById(R.id.tv_battery_level_digit);
66         percentView = (TextView) findViewById(R.id.percent_view);
67
68         mWindowManager = this.getWindowManager();
69         windowWidth = mWindowManager.getDefaultDisplay().getWidth();
70
71         IntentFilter filter = new IntentFilter();
72         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
73         registerReceiver(mBatteryStatuReceiver, filter);
74     }
75 }

以下是简单的布局文件

主体是LinearLayout,放一个TextView来显示电池状态,一个TextView来显示电量百分比

一个简陋的电量条,拿TextView来冒充的

 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:orientation="vertical" >
 6
 7     <TextView
 8         android:id="@+id/tv_charge"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="Detecting..."
12         android:textSize="20sp"
13         />
14
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal" >
19
20         <TextView
21             android:id="@+id/tv_battery_level"
22             android:layout_width="wrap_content"
23             android:layout_height="match_parent"
24             android:text="@string/battery_level"
25             android:textSize="20sp"/>
26
27         <TextView
28             android:id="@+id/tv_battery_level_digit"
29             android:layout_width="match_parent"
30             android:layout_height="match_parent"
31             android:textSize="20sp"/>
32     </LinearLayout>
33
34     <TextView
35         android:id="@+id/percent_view"
36         android:layout_height="20dp"
37         android:layout_width="wrap_content"
38         android:background="#00AA00"
39         />
40 </LinearLayout>

时间: 2024-08-29 20:54:22

Android - 电池状态的相关文章

Android电池状态监听

如果要监听电池的状态改变,需要动态注册:android.intent.action.BATTERY_CHANGED,收到Action后可以根据对应的Key获取你需要的信息,更详细信息可以参考以下例子中的BatteryChangedReceiver类 package com.example.charginganimation; import android.app.Activity; import android.content.BroadcastReceiver; import android.

android 获得电池状态

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientati

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 a

Android电池信息获取

Android 可以通过BroadcastReceiver来获取电池信息改变的广播(ACTION_BATTERY_CHANGED),从而获取到相关的电池信息. 电池信息,及其对应的相关常数(参考网址:http://blog.sina.com.cn/s/blog_5d2e69770102vh59.html) 电池信息 类型 备注 status int 取得电池的状态,返回的状态类型由 android.os.BatteryManager 类定义的常量所决定,包括: 电池充电状态( BATTERY_S

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

本文转载自:http://blog.csdn.net/xubin341719/article/details/8498580 上一篇我们讲了锂电池的充放电的流程和电池的一些特性,这一节我们重点说一下android关机充电是怎么.充电画面显示是怎么实现的,这个在工作中也比较有用,我们开始做这一块的时候也走了不少的弯路.我记得我们做adnroid2.3的时候,关机状态和充电logo显示是在uboot中做的.应该是有两种做法,回头我再看下uboot中做画面显示那一块是怎么做的,这一节我们重点说系统中的

关于Android电池管理系统(一)Linux驱动部分

一.概述 android系统电池部分的驱动程序,继承了传统linux系统下的Power Supply驱动程序架构,Battery驱动程序通过Power Supply驱动程序生成相应的sys文件系统,从而向用户空间提供电池各种属性的接口.Linux标准的 Power Supply驱动程序所使用的文件系统路径为:/sys/class/power_supply ,其中的每个子目录表示一种能源供应设备. 二.驱动头文件 Power Supply驱动程序头文件kernel/include/linux/po

几个有趣的WEB设备API 前端提高B格必备(一)——电池状态&amp;震动api

受到同事启发,突然发现了几个有趣又实用的web api,没想到前端还有这么多有趣的东西可以玩~~简直过分. 1.电池状态API navigator.getBattery():这个api返回的是一个promise对象,会给出一个BatteryManager对象,对象中包含了:设备是否在充电,电量,以及还需充电时长和剩余时长等信息. chrome浏览器.安卓的webview.iphone都可以使用.ie,safari不管是pc还是移动端都不支持. 调用方法如下: navigator.getBatte

StickyBroadcast在获取电池状态中的妙用

StickyBroadcast在获取电池状态中的妙用 今天在做开发的时候,突然遇到这样的一个问题,当你的设备电量低于15%的时候这个时候设备的闪光灯是无法打开的,但是我们平台的解决方案对这一块没有做出任何的提示,于是直接导致了用户认为他的闪光灯坏掉了,于是老大要求我们解决这个问题. 我们都知道电池的电量信息获取我们是通过广播来实现的. 标准做法如下: private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

android电池管理系统

原文:http://www.2cto.com/kf/201408/326462.html 1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统对电源管理的合理与否直接影响到电池的续航能力,而电池系统作为其中的一部分,主要用于对电池状态的监控(电池电量.电池状态及电池温度等).下面将详细分析android的电池系统架构. 2.Android电池系统架构 Android系统中对电池的管理驱动层继承了linux下的power supply cl