监听电量和充电状态的方法,电量监控

当你在改换后台更新频率来压缩那个改正对电瓶寿命的熏陶时,检查当前电量和充电状态是二个好的最初。

自身正是贰个播放采纳器(布罗兹castReceiver),由此大家开始时代谋算直接用那几个Widget采取ACTION_BATTERY_CHANGED广播并作出反应,但却开掘那一个选择器并不会采用电池更新的广播(无论大家在布局文件中配置与否)。所以一定要另寻出路,大家想到,能够动用二个独门的为本Widget服务的Service,该Service检验到电瓶电量更新后就更新桌面包车型客车Widget显示。

电瓶寿命通过剩余电量和充电状态来影响使用改善的进行。当用沟通电充电时,实践更新操作对配备的熏陶是开玩笑的,所以在大大多案例里,你可以把改进频率调到最快。要是设备不在充电,裁减更新频率能够扶植延长电瓶寿命。

Broadcast Action: This is a sticky broadcast containing the charging
state, level, and other information about the battery. See
BatteryManager for documentation on the contents of the Intent.

好像的,你能够检查电瓶剩余电量等级,在电量低时,应该裁减更新频率竟然停止更新。

You can not receive this through components declared in manifests, only
by explicitly registering for it with Context.registerReceiver(). See
ACTION_BATTERY_LOW, ACTION_BATTERY_OKAY, ACTION_POWER_CONNECTED,
and ACTION_POWER_DISCONNECTED for distinct battery-related broadcasts
that are sent and can be received through manifest receivers.

注:此处的更新,指的是临近发送心跳包的动作,大概准期更新内容。并非只是指更新应用版本。如若是客商动作,比方翻页刷新,不须要基于电量和充电状态管理。

This is a protected intent that can only be sent by the system.

看清当前充电状态
透过剖断当前充电状态开头。BatteryManager会通过一个intent广播具有电瓶和充电详细情形,蕴含充电状态。

package com.billhoo.study;

因为那是三个sticky intent,你没有要求登记广播选取器。轻易地因此调用
registerReceiver,像下边包车型地铁代码段传入一个null的接纳器,当前电瓶状态的intent就能回来。你也得以流传三个实打实的接受器对象,但大家不常不会操作更新,所以那是没供给的。

import android.app.Notification;  //通知,即突显在显示屏左上角的小Logo

复制代码 代码如下:

import android.app.NotificationManager;

IntentFilter ifilter = new
IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
//你能够读到充电状态,假如在充电,能够读到是usb依然交换电
 
// 是或不是在充电
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS,
-1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
||
                     status == BatteryManager.BATTERY_STATUS_FULL;
 
// 怎么充
int chargePlug =
batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug ==
BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

import android.app.PendingIntent;

平日说来你应当在使用沟通电充电时最大化后台更新频率,在应用usb充电时降低,不充电时更低。

import android.app.Service;

监听充电状态的更换

import android.content.BroadcastReceiver;

充电状态非常轻便退换(插入/拔出充电器卡塔尔(قطر‎,所以监听充电状态,改良刷新频率很珍爱。

import android.content.Context;

充电状态改造时,BatteryManager会发一个广播。接纳这么些事件很关键,以至在选择尚未运维的时候,因为可能你要求后台开启更新服务。所以,在Androidmanifest.xml里登记广播选取器,加上四个action:ACTION_POWER_CONNECTED
和ACTION_POWER_DISCONNECTED作为过滤。

import android.content.Intent;

复制代码 代码如下:

import android.content.IntentFilter;

<receiver android:name=”.PowerConnectionReceiver”>
  <intent-filter>
    <action
android:name=”android.intent.action.ACTION_POWER_CONNECTED”/>
    <action
android:name=”android.intent.action.ACTION_POWER_DISCONNECTED”/>
  </intent-filter>
</receiver>

import android.os.BatteryManager;

在事关的播放接受器达成里,你能够读出当前充电状态,方法跟上一步说的相仿:

import android.os.IBinder;

复制代码 代码如下:

public class MonitorService extends Service {

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
-1);
        boolean isCharging = status ==
BatteryManager.BATTERY_STATUS_CHARGING ||
                            status ==
BatteryManager.BATTERY_STATUS_FULL;
 
        int chargePlug =
intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug ==
BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug ==
BatteryManager.BATTERY_PLUGGED_AC;
    }
}

@Override

剖断当前剩下电量

public IBinder onBind(Intent intent) {

在少数案例里,剖断当前剩余电量等效很有用。假使电量在一些水平之下,你大概会选用减少后台更新频率。
你能够用下边的代码读到电量:

return null;

复制代码 代码如下:

}

//当前剩余电量
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,
-1);
//电量最大值
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE,
-1);
//电量百分比
float batteryPct = level / (float)scale;

@Override

注:近期不清楚干什么要如此算,在本身要好的机器上运转,scale正是100的。

public void onStart(Intent intent, int startId) {

**监听剩余电量分明改动

//
定义电瓶电量更新广播的过滤器,只接收带有ACTION_BATTERRY_CHANGED事件的Intent

**持续监听电瓶状态不易于,但你不用如此做。
近似的话,持续监听电瓶电量对电瓶的震慑比app的例行行为还要大。所以,只监听剩余电量的钦赐等第的改良(步向或离开低电量状态卡塔尔国是三个很好的推行。
manifest里声称的选拔器,会在步入或离开低电量状态时触发。

IntentFilter batteryChangedReceiverFilter = new IntentFilter();

复制代码 代码如下:

batteryChangedReceiverFilter.addAction(Intent.ACTION_BATTERY_CHANGED);

<receiver android:name=”.BatteryLevelReceiver”>
<intent-filter>
  <action
android:name=”android.intent.action.ACTION_BATTERY_LOW”/>
  <action
android:name=”android.intent.action.ACTION_BATTERY_OKAY”/>
  </intent-filter>
</receiver>

// 向系统注册batteryChangedReceiver选拔器,本选取器的落实见代码字段处

结余电量严重不足时,最佳禁止使用全部后台更新。在你能够接收手提式无线电话机在此之前就关机了,这种情状下,假若刷新数据并不主要。
在不菲景色下,设备是是插入到底座里充电的(好啊,反正笔者没见多少人格外花钱买底座的,可能海外超多卡塔尔(英语:State of Qatar)。下节讲怎么判别当前底座状态和监听插入底座时退换。作品链接://www.jb51.net/article/51557.htm

registerReceiver(batteryChangedReceiver, batteryChangedReceiverFilter);

您大概感兴趣的稿子:

  • android
    电话状态监听(来电和去电卡塔尔实现代码
  • Android应用中Back键的监听及处理实例
  • Android中达成监听ScrollView滑动事件
  • Android中监听Home键的4种方法总计
  • android中采纳Activity达成监听手指上下左右滑动
  • Android中监听判别互联网连接状态的章程
  • android
    Animation监听器AnimationListener的应用方法卡塔尔(英语:State of Qatar)
  • 周到拆解深入分析Android中对EditText输入完结监听的法子
  • Android控件连串之Button以致Android监听器使用介绍
  • Android自定义ScrollView使用自定义监听

// 实例化Notification布告的微型机,即字段notification manager

notifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

//
由于伊始化本服务时系统恐怕未有生出ACTION_BATTERY_CHANGED广播,那么刚才注册的不得了选择器将不会在本服务运维时被激活,这种情景下就不能够出示当前电量,由此在这里丰富二个无名氏广播选取器。

new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

int level = intent.getIntExtra(“level”, 0卡塔尔(英语:State of Qatar);  //电瓶电量品级

int scale = intent.getIntExtra(“scale”, 100卡塔尔国;  //电池满时百分比

int status = intent.getIntExtra(“status”, 0卡塔尔;  //电瓶状态

// 若正在充电

if (status == BatteryManager.BATTERY_STATUS_CHARGING)

notification = getNotification(getChargingIcon(level * 100 / scale),

“电池监察和控制”, System.currentTimeMillis(卡塔尔(قطر‎, “电瓶电量”, “正在充电”卡塔尔(英语:State of Qatar);

else

notification = getNotification(getStateIcon(level * 100 / scale),

“电瓶监控”, System.current提姆eMillis(卡塔尔国, “电瓶电量”, “请马上充电”卡塔尔国;

//向系统一发布送此布告,为实惠起见,通知ID简单设为0

notifyManager.notify(0, notification);

}

};

}

@Override

public void onDestroy() {

//当本服务终止时,通过文告ID注销左上角的打招呼

notifyManager.cancel(0);

}

//
————————————–私有主意——————————

// 获得三个notification

private Notification getNotification(int notificationIcon, String
tickerText,

long when, String contentTitle, String contentText) {

// 定义该notification的扩展文本和intent

Notification notification = new Notification(notificationIcon,
tickerText,

when);

Context context = getApplicationContext();

Intent notificationIntent = new Intent(context, BatteryInfo.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText,

contentIntent);

// 设置本状态出今后“正在拓宽”而非“布告”栏目,不许客户解除状态Logo

notification.flags |= Notification.FLAG_ONGOING_EVENT;

return notification;

}

// 得到相应的电瓶状态Logo

private int getStateIcon(int batteryHealth) {

if (batteryHealth >= 0 || batteryHealth <= 100)

return batteryStateIcons[batteryHealth];
//这里是为了有援救起见而硬编码的财富数组,直接从Wrangler.java文件中领到的十七进制财富ID

return R.drawable.stat_sys_battery_unknown;

}

// 拿到相应的充电状态Logo

private int getChargingIcon(int batteryHealth) {

if (batteryHealth >= 0 && batteryHealth < 5)

return R.drawable.stat_sys_battery_charge_anim0;

if (batteryHealth >= 5 && batteryHealth < 10)

return R.drawable.stat_sys_battery_charge_anim01;

if (batteryHealth >= 10 && batteryHealth < 15)

return R.drawable.stat_sys_battery_charge_anim02;

if (batteryHealth >= 15 && batteryHealth < 20)

return R.drawable.stat_sys_battery_charge_anim03;

if (batteryHealth >= 20 && batteryHealth < 25)

return R.drawable.stat_sys_battery_charge_anim04;

if (batteryHealth >= 25 && batteryHealth < 30)

return R.drawable.stat_sys_battery_charge_anim05;

if (batteryHealth >= 30 && batteryHealth < 35)

return R.drawable.stat_sys_battery_charge_anim06;

if (batteryHealth >= 35 && batteryHealth < 40)

return R.drawable.stat_sys_battery_charge_anim07;

if (batteryHealth >= 40 && batteryHealth < 45)

return R.drawable.stat_sys_battery_charge_anim08;

if (batteryHealth >= 45 && batteryHealth < 50)

return R.drawable.stat_sys_battery_charge_anim09;

if (batteryHealth >= 50 && batteryHealth < 55)

return R.drawable.stat_sys_battery_charge_anim10;

if (batteryHealth >= 55 && batteryHealth < 60)

return R.drawable.stat_sys_battery_charge_anim11;

if (batteryHealth >= 60 && batteryHealth < 65)

return R.drawable.stat_sys_battery_charge_anim12;

if (batteryHealth >= 65 && batteryHealth < 70)

return R.drawable.stat_sys_battery_charge_anim13;

if (batteryHealth >= 70 && batteryHealth < 75)

return R.drawable.stat_sys_battery_charge_anim14;

if (batteryHealth >= 75 && batteryHealth < 80)

return R.drawable.stat_sys_battery_charge_anim15;

if (batteryHealth >= 80 && batteryHealth < 85)

return R.drawable.stat_sys_battery_charge_anim16;

if (batteryHealth >= 85 && batteryHealth < 90)

return R.drawable.stat_sys_battery_charge_anim17;

if (batteryHealth >= 90 && batteryHealth < 95)

return R.drawable.stat_sys_battery_charge_anim18;

if (batteryHealth >= 95 && batteryHealth < 100)

return R.drawable.stat_sys_battery_charge_anim19;

if (batteryHealth == 100)

return R.drawable.stat_sys_battery_charge_animfull;

return R.drawable.stat_sys_battery_unknown;

}

//
——————————-私有字段————————————–

private NotificationManager notifyManager = null;

private Notification notification = null;

//这里是为着便于起见而硬编码的icon财富数组,直接从CRUISER.java文件中领取的十一进制能源ID,本数组的0号成分0x7f020012表示能源stat_sys_battery_0.png(见附属类小零器件),以往累计。差异配置的ID只怕分裂,请读者自行改正。

private int batteryStateIcons[] = { 0x7f020002, 0x7f020003,
0x7f020004,

0x7f020005, 0x7f020006, 0x7f020007, 0x7f020008, 0x7f020009, 0x7f02000a,

0x7f02000b, 0x7f02000c, 0x7f02000d, 0x7f02000e, 0x7f02000f, 0x7f020010,

0x7f020011, 0x7f020012, 0x7f020013, 0x7f020014, 0x7f020015, 0x7f020016,

0x7f020017, 0x7f020018, 0x7f020019, 0x7f02001a, 0x7f02001b, 0x7f02001c,

0x7f02001d, 0x7f02001e, 0x7f02001f, 0x7f020020, 0x7f020021, 0x7f020022,

0x7f020023, 0x7f020024, 0x7f020025, 0x7f020026, 0x7f020027, 0x7f020028,

0x7f020029, 0x7f02002a, 0x7f02002b, 0x7f02002c, 0x7f02002d, 0x7f02002e,

0x7f02002f, 0x7f020030, 0x7f020031, 0x7f020032, 0x7f020033, 0x7f020034,

0x7f020035, 0x7f020036, 0x7f020037, 0x7f020038, 0x7f020039, 0x7f02003a,

0x7f02003b, 0x7f02003c, 0x7f02003d, 0x7f02003e, 0x7f02003f, 0x7f020040,

0x7f020041, 0x7f020042, 0x7f020043, 0x7f020044, 0x7f020045, 0x7f020046,

0x7f020047, 0x7f020048, 0x7f020049, 0x7f02004a, 0x7f02004b, 0x7f02004c,

0x7f02004d, 0x7f02004e, 0x7f02004f, 0x7f020050, 0x7f020051, 0x7f020052,

0x7f020053, 0x7f020054, 0x7f020055, 0x7f020056, 0x7f020057, 0x7f020058,

0x7f020059, 0x7f02005a, 0x7f02005b, 0x7f02005c, 0x7f02005d, 0x7f02005e,

0x7f02005f, 0x7f020060, 0x7f020061, 0x7f020062, 0x7f020063, 0x7f020064,

0x7f020065, 0x7f02007b };

// 选拔电池新闻更新的广播

private BroadcastReceiver batteryChangedReceiver = new
BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {

int level = intent.getIntExtra(“level”, 0);

int scale = intent.getIntExtra(“scale”, 100);

int status = intent.getIntExtra(“status”, 0);

// 若正在充电

if (status == BatteryManager.BATTERY_STATUS_CHARGING)

notification = getNotification(getChargingIcon(level * 100 / scale),

“Battery Monitor”, System.currentTime米尔is(卡塔尔, “电瓶电量”, “正在充电”卡塔尔;

else

notification = getNotification(getStateIcon(level * 100 / scale),

“Battery Monitor”, System.currentTimeMillis(卡塔尔, “电瓶电量”,
“请登时充电”卡塔尔国;

notifyManager.notify(0, notification);

}

};

}

Post Author: admin

发表评论

电子邮件地址不会被公开。 必填项已用*标注