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

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

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

比如 BatteryManager.EXTRA_STATUS     BatteryManager.BATTERY_STATUS_CHARGING  等等

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

 package com.example.chargingwatching;

 import android.app.Activity;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.BatteryManager;
 import android.os.Bundle;
 import android.view.WindowManager;
 import android.widget.TextView;

 public class MainActivity extends Activity {
     private TextView chargeStatus;
     private TextView LevelDigit;
     private TextView percentView;
     private int rustLevel = 0;    /* battery level */
     private int windowWidth = 0;
     private WindowManager mWindowManager;

     private BroadcastReceiver mBatteryStatuReceiver = new BroadcastReceiver() {
         int status;    // current battery status
         int plugType;
         public void onReceive(Context context, Intent intent)    {

             rustLevel = (int)(100f
                     * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                     / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));

             LevelDigit.setText("  " + rustLevel + "%");

             percentView.setWidth((int)(windowWidth * rustLevel /100));
             status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
                     BatteryManager.BATTERY_STATUS_UNKNOWN);
             plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

             if(status == BatteryManager.BATTERY_STATUS_CHARGING) {
                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
                     chargeStatus.setText(R.string.ac_charging);
                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
                     chargeStatus.setText(R.string.usb_charging);
                 }
             } else if (status == BatteryManager.BATTERY_STATUS_FULL) {
                 if(plugType == BatteryManager.BATTERY_PLUGGED_AC) {
                     chargeStatus.setText(R.string.full_ac);
                 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
                     chargeStatus.setText(R.string.full_usb);
                 }
             } else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                 chargeStatus.setText(R.string.uncharge);
             } else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
                 chargeStatus.setText(R.string.discharging);
             } else {
                 chargeStatus.setText(R.string.unknown);
             }
         }
     };

     @SuppressWarnings("deprecation")
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.watch_charging);
         chargeStatus = (TextView)findViewById(R.id.tv_charge);
         LevelDigit = (TextView) findViewById(R.id.tv_battery_level_digit);
         percentView = (TextView) findViewById(R.id.percent_view);

         mWindowManager = this.getWindowManager();
         windowWidth = mWindowManager.getDefaultDisplay().getWidth();

         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
         registerReceiver(mBatteryStatuReceiver, filter);
     }
 }

以下是简单的布局文件

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

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

 <?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:orientation="vertical" >

     <TextView
         android:id="@+id/tv_charge"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Detecting..."
         android:textSize="20sp"
         />

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal" >

         <TextView
             android:id="@+id/tv_battery_level"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:text="@string/battery_level"
             android:textSize="20sp"/>

         <TextView
             android:id="@+id/tv_battery_level_digit"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:textSize="20sp"/>
     </LinearLayout>

     <TextView
         android:id="@+id/percent_view"
         android:layout_height="20dp"
         android:layout_width="wrap_content"
         android:background="#00AA00"
         />
 </LinearLayout>

Android - 电池状态的更多相关文章

  1. Android监听电池状态

    监听电池状态只需要接收Intent.ACTION_BATTERY_CHANGED的广播即可,当电池状态发生变化时会发出广播. 1.运行状态如下图: (1)连接USB时的状态 (2)断开USB时的状态 ...

  2. android 获得电池状态

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  3. Android虚拟机电池状态设置

    问题描述: 安装SDK后使用AVD配合APPIUM进行测试,此时虚拟机的电池状态为0%充电中:部分APP会对手机电池状态有要求,不符合要求时,无法安装或打开. 解决思路: 1.Android系统设置( ...

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

    受到同事启发,突然发现了几个有趣又实用的web api,没想到前端还有这么多有趣的东西可以玩~~简直过分. 1.电池状态API navigator.getBattery():这个api返回的是一个pr ...

  5. android电池管理系统从上层的java到底层驱动的调用(转载)

    1.概述 随着移动智能设备的快速发屏,电池的续航能力在很大情况下诱导了大众消费者的购买选择,android系统对电源管理的合理与否直接影响到电池的续航能力,而电池系统作为其中的一部分,主要用于对电池状 ...

  6. android电池信息简介

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. 【转】android电池(五):电池 充电IC(PM2301)驱动分析篇

    关键词:android 电池  电量计  PL2301任务初始化宏 power_supply 中断线程化 平台信息:内核:linux2.6/linux3.0系统:android/android4.0  ...

  8. 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇

    关键词:android 电池  电量计  MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...

  9. 【转】android 电池(三):android电池系统

    关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...

随机推荐

  1. 基于angular实现模拟微信小程序swiper组件

    这段时间的主业是完成一个家政类小程序,终于是过审核发布了.不得不说微信的这个小程序生态还是颇有想法的,抛开他现有的一些问题不说,其提供的组件系统乍一看还是蛮酷的.比如其提供的一个叫swiper的视图组 ...

  2. 玩转nodeJS系列:使用原生API实现简单灵活高效的路由功能(支持nodeJs单机集群),nodeJS本就应该这样轻快

    前言: 使用nodeJS原生API实现快速灵活路由,方便与其他库/框架进行整合: 1.原生API,简洁高效的轻度封装,加速路由解析,nodeJS本就应该这样轻快 2.不包含任何第三方库/框架,可以灵活 ...

  3. PHP验证码的制作教程

    自己过去自学了PHP绘画验证码的教程,现在就把这一部分笔记跟大家分享,希望可以帮到大家. 顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度云 ...

  4. 【iOS】UIDynamicAnimator动画

    创建动画 UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; 协议代理 ...

  5. nginx之 nginx-1.9.7 + tomcat-8.5.15 反向代理+应用负载均衡 安装配置

    环境说明:nginx 反向代理服务器 ip 为: 10.219.24.26tomcat1 应用服务器 ip 为: 10.219.24.21tomcat3 应用服务器 ip 为: 10.219.24.2 ...

  6. [0] 关于IComparable和IComparer接口和Comparer类

    关于IComparable和IComparer接口 和 Comparer类 IComparable和ICompareframeworkr接口是.net 中比较对象的标准方式,这两个接口之间的区别如下: ...

  7. RSA加密通信小结(三)--生成加解密所需的SSL命令与流程

    在iOS中使用RSA加密解密,需要用到.der和.p12后缀格式的文件,其中.der格式的文件存放的是公钥(Public key)用于加密,.p12格式的文件存放的是私钥(Private key)用于 ...

  8. angularjs下拉框空白

    搜索angularjs下拉框空白,可以出现很多解决方案,但是对于静态字段来说,网上目前还没有找到解决方案,如下: <select class="form-control" n ...

  9. 深入理解YYCache

    前言 本篇文章将带来YYCache的解读,YYCache支持内存和本地两种方式的数据存储.我们先抛出两个问题: YYCache是如何把数据写入内存之中的?又是如何实现的高效读取? YYCache采用了 ...

  10. StructureMap经典的IoC/DI容器

    StructureMap是一款很老的IoC/DI容器,从2004年.NET 1.1支持至今. 一个使用例子 //创建业务接口 public interface IDispatchService { } ...