android 监听手机电量变化
今天,簡(jiǎn)單講講如何監(jiān)聽手機(jī)電量的變化。
監(jiān)聽電量是不能靜態(tài)注冊(cè)的。
后來上網(wǎng)搜索,發(fā)現(xiàn)有五個(gè)不能靜態(tài)注冊(cè)的廣播,這里記錄一下,免得下次再后知后覺的發(fā)現(xiàn)并驚訝于自己的笨拙。
不能靜態(tài)注冊(cè)的廣播:
android.intent.action.SCREEN_ON
android.intent.action.SCREEN_OFF
android.intent.action.BATTERY_CHANGED
android.intent.action.CONFIGURATION_CHANGED
android.intent.action.TIME_TICK
原因(有以下幾種說法,提供給大家參考):
1.提高系統(tǒng)效率:這兩個(gè)事件是android的基本事件,如果大多數(shù)程序監(jiān)聽,會(huì)大大的拖慢整個(gè)系統(tǒng),所以android不鼓勵(lì)我們?cè)诤笈_(tái)監(jiān)聽這兩個(gè)事件。
2.因?yàn)橛行驈V播的優(yōu)先級(jí)問題。以上這些廣播中,靜態(tài)注冊(cè)時(shí),系統(tǒng)的優(yōu)先級(jí)大于應(yīng)用,并且系統(tǒng)阻止了廣播的向下傳播。又因在Android 的廣播機(jī)制中,動(dòng)態(tài)注冊(cè)的優(yōu)先級(jí)是要高于靜態(tài)注冊(cè)優(yōu)先級(jí)的。故用動(dòng)態(tài)注冊(cè)代替靜態(tài)注冊(cè)。
3.系統(tǒng)安全問題。
Intent.ACTION_BATTERY_CHANGED
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.
你不能像組件那樣在manifests里聲明一個(gè)receive ,你只能通過Context.registerReceiver()注冊(cè)。. 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.
Constant Value:?"android.intent.action.BATTERY_CHANGED"
BatteryManager類列出了該intent的extra所包含的信息: String?EXTRA_HEALTH?: integer.它表示當(dāng)前電池的健康狀態(tài)。 可能的值為: int?BATTERY_HEALTH_COLD? int?BATTERY_HEALTH_DEAD? int?BATTERY_HEALTH_GOOD int?BATTERY_HEALTH_OVERHEAT int?BATTERY_HEALTH_OVER_VOLTAGE int?BATTERY_HEALTH_UNKNOWN int?BATTERY_HEALTH_UNSPECIFIED_FAILURE String?EXTRA_ICON_SMALL?: integer.the resource ID of a small status bar icon indicating the current battery state. 它是當(dāng)前用于表示電池狀態(tài)的icon的資源id. String?EXTRA_LEVEL: integer.它表示電池當(dāng)前的電量, 它介于0和?EXTRA_SCALE之間. String?EXTRA_PLUGGED?: integer.它表示當(dāng)前手機(jī)使用的是哪里的電源。 可能的值有: 0:表示電源是電池 int?BATTERY_PLUGGED_AC?:表示電源是AC charger.[應(yīng)該是指充電器]。 int?BATTERY_PLUGGED_USB?:表示電源是USB port. String?EXTRA_PRESENT: boolean.表示是否提供電池。有些手機(jī)在使用USB電源的情況下,即使拔出了電池,仍然可以正常工作。 String?EXTRA_SCALE: integer.表示電池電量的最大值. String?EXTRA_STATUS?: integer。表示電池的當(dāng)前狀態(tài)。 可能的值為: int?BATTERY_STATUS_CHARGING?表示正在充電。 int?BATTERY_STATUS_DISCHARGING? int?BATTERY_STATUS_FULL?表示充滿 int?BATTERY_STATUS_NOT_CHARGING?表示沒有充電 int?BATTERY_STATUS_UNKNOWN表示未知狀態(tài)。 String?EXTRA_TECHNOLOGY?:表示電池使用的技術(shù)。比如,對(duì)于鋰電池是Li-ion. String?EXTRA_TEMPERATURE:integer 。表示當(dāng)前電池的溫度。? String?EXTRA_VOLTAGE?: integer。表示當(dāng)前電池的電壓。.
另外還有兩種intent專門用于表示電量低的情況 ACTION_BATTERY_LOW :表示當(dāng)前電池電量低。 ACTION_BATTERY_OKAY:表示當(dāng)前電池已經(jīng)從電量低恢復(fù)為正常。
當(dāng)手機(jī)電量發(fā)生改變時(shí),系統(tǒng)會(huì)對(duì)外發(fā)送Intent的Action為android.intent.action.BATTERY_CHANGED常量的廣播;當(dāng)手機(jī)電量過低時(shí),系統(tǒng)會(huì)對(duì)外發(fā)送Intent的Action為android.intent.action.BATTERY_LOW常量的廣播。
當(dāng)手機(jī)電池從電量不足狀態(tài)恢復(fù)時(shí),系統(tǒng)會(huì)對(duì)外發(fā)送Intent的Action為android.intent.action.BATTERY_OKAY常量的廣播。
下面通過一個(gè)簡(jiǎn)單實(shí)例來演示:
Receiver:
package com.home.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class BatteryReceiver extends BroadcastReceiver { @Overridepublic void onReceive(Context context, Intent intent) { if (Intent.ACTION_BATTERY_OKAY.equals(intent.getAction())) { Toast.makeText(context, "電量已恢復(fù),可以使用!", Toast.LENGTH_LONG).show(); } if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())) { Toast.makeText(context, "電量過低,請(qǐng)盡快充電!", Toast.LENGTH_LONG).show(); } if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) { Bundle bundle = intent.getExtras(); // 獲取當(dāng)前電量 int current = bundle.getInt("level"); // 獲取總電量 int total = bundle.getInt("scale"); StringBuffer sb = new StringBuffer(); sb.append("當(dāng)前電量為:" + current * 100 / total + "%" + " "); // 如果當(dāng)前電量小于總電量的15% if (current * 1.0 / total < 0.15) { sb.append("電量過低,請(qǐng)盡快充電!"); } else { sb.append("電量足夠,請(qǐng)放心使用!"); } Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show(); } } }
配置Receiver:
receiver android:name="com.home.receiver.BatteryReceiver"> <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED" /> <action android:name="android.intent.action.BATTERY_OKAY"/> <action android:name="android.intent.action.BATTERY_LOW"/> </intent-filter> </receiver>
在清單文件添加權(quán)限
<uses-permission android:name="android.permission.BATTERY_STATS"/>
這里可能需要注意一下,?<action?android:name="android.intent.action.BATTERY_CHANGED"?/>??靜態(tài)注冊(cè)可能沒有效果,版本較低的可能才有效果,版本叫高的只能動(dòng)態(tài)注冊(cè)。
android 監(jiān)聽手機(jī)電量變化就講完了。
就這么簡(jiǎn)單。
總結(jié)
以上是生活随笔為你收集整理的android 监听手机电量变化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 监听手机开机
- 下一篇: android 对for循环进行优化