博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android - 电池状态
阅读量:6971 次
发布时间:2019-06-27

本文共 3823 字,大约阅读时间需要 12 分钟。

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

电池的信息,一般都在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 status23         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     @Override61     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 
2
6 7
14 15
19 20
26 27
32
33 34
40

 

转载地址:http://dnosl.baihongyu.com/

你可能感兴趣的文章
CF1072A Palindromic Twist 思维
查看>>
Leetcode c语言-Permutations
查看>>
《javascript设计模式》阅读笔记
查看>>
JQuery基础总结上
查看>>
postgresql 备份
查看>>
Cocos2d-x调用Java 代码
查看>>
close方法
查看>>
如何为crontab调度运行的多脚本设置共享的环境变量?
查看>>
bash字符串匹配
查看>>
选择设置好ext3日志模式
查看>>
程序员专用经典语录
查看>>
网络爬虫
查看>>
iOS开发基础
查看>>
5 创建型模式-----原型模式
查看>>
C++:vector中的resize()函数 VS reserve()函数
查看>>
JS对象和数组
查看>>
CodeForces - 1105C (dp)
查看>>
安装服务windows,installutil
查看>>
斐波那契数列
查看>>
如何在android程序中使用百度api接口:
查看>>