手机电量发生改变时,系统会对放发送Intent的Action为android.intent.action.BATTERY_CHANGED,这个可以在AndroidManifest.xml中配置

                                                                    

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 {@Overridepublic 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();}}}

注意这两行代码的使用

//获取当前电量int current = bundle.getInt("level");//获取总电量 int total = bundle.getInt("scale");


更多相关文章

  1. Android 获取手机电量信息
  2. 降低Android应用程序的耗电量
  3. android EditText setText发生stackoerverflowerror
  4. 如何降低android应用程序的耗电量
  5. Android 解决多个通知发生冲突的问题
  6. 转载 Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息
  7. Android:系统信息(内存、cpu、sd卡、电量、版本)的获取
  8. android电池(四):电池 电量计(MAX17040)驱动分析篇

随机推荐

  1. android6.0 状态栏添加图标 举例Location
  2. Android(安卓)stuido 快捷键
  3. Android(安卓)Studio编译时Gradle报乱码
  4. android默认debug.keystore的密码
  5. android平台开发问题小结----今天遇到的
  6. android中json解析及使用 (下)
  7. Android(安卓)蓝牙开发浅析
  8. 关于android 经典蓝牙开发 使用UUID连接
  9. android 服务前台运行startForeground
  10. android的Menu使用(1)------选项菜单