Android监听电池状态实例代码
程序员文章站
2024-02-23 22:13:52
如果要监听电池的状态改变,需要动态注册:android.intent.action.battery_changed,收到action后可以根据对应的key获取你需要的信息,...
如果要监听电池的状态改变,需要动态注册:android.intent.action.battery_changed,收到action后可以根据对应的key获取你需要的信息,更详细信息可以参考以下例子中的batterychangedreceiver类
具体代码如下所示:
package com.example.charginganimation; 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.view; public class mainactivity extends activity { private customcliploading ccl; private batterychangedreceiver receiver = new batterychangedreceiver(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); registerreceiver(receiver, getfilter()); } private intentfilter getfilter() { intentfilter filter = new intentfilter(); filter.addaction(intent.action_battery_changed); filter.addaction(intent.action_battery_low); filter.addaction(intent.action_battery_okay); return filter; } @override protected void ondestroy() { // todo auto-generated method stub unregisterreceiver(receiver); super.ondestroy(); } class batterychangedreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub final string action = intent.getaction(); if (action.equalsignorecase(intent.action_battery_changed)) { system.out .println("batterychangedreceiver battery_changed_action---"); // 当前电池的电压 int voltage = intent.getintextra(batterymanager.extra_voltage, -1); // 电池的健康状态 int health = intent .getintextra(batterymanager.extra_health, -1); switch (health) { case batterymanager.battery_health_cold: system.out.println("battery_health_cold"); break; case batterymanager.battery_health_dead: system.out.println("battery_health_dead "); break; case batterymanager.battery_health_good: system.out.println("battery_health_good"); break; case batterymanager.battery_health_overheat: system.out.println("battery_health_overheat"); break; case batterymanager.battery_health_over_voltage: system.out.println("battery_health_cold"); break; case batterymanager.battery_health_unknown: system.out.println("battery_health_unknown"); break; case batterymanager.battery_health_unspecified_failure: system.out.println("battery_health_unspecified_failure"); break; default: break; } // 电池当前的电量, 它介于0和 extra_scale之间 int level = intent.getintextra(batterymanager.extra_level, -1); // 电池电量的最大值 int scale = intent.getintextra(batterymanager.extra_scale, -1); // 当前手机使用的是哪里的电源 int pluged = intent.getintextra(batterymanager.extra_plugged, -1); switch (pluged) { case batterymanager.battery_plugged_ac: // 电源是ac charger.[应该是指充电器] system.out.println("battery_plugged_ac"); break; case batterymanager.battery_plugged_usb: // 电源是usb port system.out.println("battery_plugged_usb "); break; default: break; } int status = intent .getintextra(batterymanager.extra_status, -1); switch (status) { case batterymanager.battery_status_charging: // 正在充电 system.out.println("battery_status_charging "); break; case batterymanager.battery_status_discharging: system.out.println("battery_status_discharging "); break; case batterymanager.battery_status_full: // 充满 system.out.println("battery_status_full "); break; case batterymanager.battery_status_not_charging: // 没有充电 system.out.println("battery_status_not_charging "); break; case batterymanager.battery_status_unknown: // 未知状态 system.out.println("battery_status_unknown "); break; default: break; } // 电池使用的技术。比如,对于锂电池是li-ion string technology = intent .getstringextra(batterymanager.extra_technology); // 当前电池的温度 int temperature = intent.getintextra( batterymanager.extra_temperature, -1); system.out.println("voltage = " + voltage + " technology = " + technology + " temperature = " + temperature + " level = " + level + " scale = " + scale); } else if (action.equalsignorecase(intent.action_battery_low)) { // 表示当前电池电量低 system.out .println("batterychangedreceiver action_battery_low---"); } else if (action.equalsignorecase(intent.action_battery_okay)) { // 表示当前电池已经从电量低恢复为正常 system.out .println("batterychangedreceiver action_battery_okay---"); } } } }
获取apk和版本信息
private int getvercode(context context) { int vercode = -1; try { vercode = context.getpackagemanager().getpackageinfo( "com.example.charginganimation", 0).versioncode; } catch (namenotfoundexception e) { e.printstacktrace(); } return vercode; } private string getvername(context context) { string vername = ""; try { vername = context.getpackagemanager().getpackageinfo( "com.example.charginganimation", 0).versionname; } catch (namenotfoundexception e) { e.printstacktrace(); } return vername; }
以上代码内容是小编给大家介绍的android监听电池状态的实例代码,本文写的不好还请见谅,有好的意见欢迎给小编留言,我会及时给大家沟通的,谢谢大家一直以来对网站的支持!
上一篇: java控制台输入示例分享
下一篇: java中文转拼音工具类详解