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

电池的信息,一般都在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. Android React Native 开发环境搭建---windows下

    环境搭建 环境搭建可以参考RN官网,也可以参考中文版本:http://reactnative.cn/docs/0.45/getting-started.html 如果你希望可以看到原版的安装流程,可以 ...

  2. ap.net core 教程(三) - 新建项目

    ASP.NET Core - 新建项目 在这一章,我们将讨论如何在Visual Studio中创建一个新项目. 只要你安装了Visual Studio 2015的.net core工具,您就可以开始构 ...

  3. python爬虫番外篇(一)进程,线程的初步了解

    一.进程 程序并不能单独和运行只有将程序装载到内存中,系统为他分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别在于:程序是指令的集合,它是进程的静态描述文本:进程是程序的一次执行活动, ...

  4. NodeJS 实现手机短信验证 模块阿里大于

    1,NodeJS 安装阿里大于模块 切换到项目目录使用npm 安装阿里于模块 npm i node-alidayu --save 2,aliyu官网使用淘宝账户登录 登录阿里大于 https://do ...

  5. centos 6.6 ios镜像文件 下载 官网和阿里云两种方式教你下载

    1百度一下:centos 打开打开官网.选择这一项 CET CENTOS 2选择 DVD ISO,双击下载 直接选择左键点击下载 这里需要迅雷 方法 二 打开 https://mirrors.aliy ...

  6. CSS中的尺寸单位

    绝对单位 px: Pixel 像素 pt: Points 磅 pc: Picas 派卡 in: Inches 英寸 mm: Millimeter 毫米 cm: Centimeter 厘米 q: Qua ...

  7. python 文件操作(pickle)

    >>> with open('text.txt','wb') as data:pickle.dump(['a','b',2],data) 保存到文件 >>> wit ...

  8. webpack 实现的多入口项目脚手架

    简介 基于 webpack2 实现的多入口项目脚手架,主要使用 extract-text-webpack-plugin 实现 js .css 公共代码提取,html-webpack-plugin 实现 ...

  9. EntityFramework连接SQLite

    EF很强大,可惜对于SQLite不支持CodeFirst模式(需要提前先设计好数据库表结构),不过对SQLite的数据操作还是很好用的. 先用SQLiteManager随便创建一个数据库和一张表:

  10. Redis的安装与使用(单节点)

    IP:192.168.4.111 环境:CentOS 6.6 Redis版本:redis-3.0 (考虑到Redis3.0在集群和性能提升方面的特性,rc版为正式版的候选版,而且很快就出正式版) 安装 ...