初学Android,电池电量提示(七十五) .
2012-11-30 11:31
411 查看
/article/2580717.html
手机电量发生改变时,系统会对放发送Intent的Action为android.intent.action.BATTERY_CHANGED,这个可以在AndroidManifest.xml中配置
[html]
view plaincopyprint?
<receiver android:name="BatteryReceiver" >
<!-- 监听电池电量改变 -->
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
[java]
view plaincopyprint?
package WangLi.Service.MonitorBattery; 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 { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); //获取当前电量 int current = bundle.getInt("level"); //获取总电量 int total = bundle.getInt("scale"); //如果当前电量小于总电量的15% if(current * 1.0 / total < 0.15) { Toast.makeText(context, "电量过低,请尽快充电", 5000).show(); } } }
注意这两行代码的使用
[java]
view plaincopyprint?
//获取当前电量
int current = bundle.getInt("level");
//获取总电量
int total = bundle.getInt("scale");
手机电量发生改变时,系统会对放发送Intent的Action为android.intent.action.BATTERY_CHANGED,这个可以在AndroidManifest.xml中配置
[html]
view plaincopyprint?
<receiver android:name="BatteryReceiver" >
<!-- 监听电池电量改变 -->
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
<receiver android:name="BatteryReceiver" > <!-- 监听电池电量改变 --> <intent-filter> <action android:name="android.intent.action.BATTERY_CHANGED" /> </intent-filter> </receiver>
[java]
view plaincopyprint?
package WangLi.Service.MonitorBattery; 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 { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); //获取当前电量 int current = bundle.getInt("level"); //获取总电量 int total = bundle.getInt("scale"); //如果当前电量小于总电量的15% if(current * 1.0 / total < 0.15) { Toast.makeText(context, "电量过低,请尽快充电", 5000).show(); } } }
package WangLi.Service.MonitorBattery; 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 { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); //获取当前电量 int current = bundle.getInt("level"); //获取总电量 int total = bundle.getInt("scale"); //如果当前电量小于总电量的15% if(current * 1.0 / total < 0.15) { Toast.makeText(context, "电量过低,请尽快充电", 5000).show(); } } }
注意这两行代码的使用
[java]
view plaincopyprint?
//获取当前电量
int current = bundle.getInt("level");
//获取总电量
int total = bundle.getInt("scale");
相关文章推荐
- 初学Android,电池电量提示(七十五)
- Android根据电量变化为不同图片的方法【电池电量提示】
- unity3d 获取电量电池状态等 android and ios
- android 修改电池电量图标介绍
- Android 电池电量进度条,上下滚动图片的进度条(battery)
- Android 获取电池电量
- 转: Android 电源管理专题之监测电池电量和充电状态 | 润物无声
- 获取Android设备电池电量状态
- Android 4.4 电池电量显示相关
- Android取得电池的电量
- android电池充电以及电量检测驱动分析
- 79 Android BroadcastReceiver 电池电量监听
- Android获取手机电池电量用法实例
- Android查看电池电量的方法(基于BroadcastReceiver)
- android 监听电池电量的变化
- android 4.4 电池电量管理底层分析(C\C++层)
- android获取电池电量(无延迟)
- android 电池电量检测,充电显示效果
- Android实现侦听电池状态显示、电量及充电动态显示的方法
- android电池电量优化