接收充电状态和电量的系统广播
来源:互联网 发布:河南房卡麻将源码 编辑:程序博客网 时间:2024/05/18 02:58
public class BroadcastR_PowerListener extends BroadcastReceiver { private static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED"; private static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED"; private static final String POWER_LOG_PATH = "Cache"; private static BroadcastReceiver instance = null; private BroadcastR_PowerListener() { } public static void registerPowerListener() { if (instance == null) { instance = new BroadcastR_PowerListener(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); intentFilter.addAction(ACTION_POWER_CONNECTED); intentFilter.addAction(ACTION_POWER_DISCONNECTED); intentFilter.setPriority(Integer.MAX_VALUE); MyApplication.getInstance().registerReceiver(instance, intentFilter); } } @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { //你可以读到充电状态,如果在充电,可以读到是usb还是交流电 // 是否在充电 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; String fileName = String.format("uid_%d_power.txt", User.getUid()); int powerLevel = intent.getIntExtra("level", 0); if (usbCharge) { write(powerLevel+"", POWER_LOG_PATH, fileName); } } else if (intent.getAction().equals(ACTION_POWER_CONNECTED)) { ErrRpt.debug("powerlistener", "Power Connected."); } else if (intent.getAction().equals(ACTION_POWER_DISCONNECTED)) { ErrRpt.debug("powerlistener", "Power DisConnected."); } } private void write(String content, String directory, String fileName) { try { File dir = new File(Environment.getExternalStorageDirectory() + "/" + directory); File file = new File(dir + "/" + fileName); if (!dir.exists()) { dir.mkdirs(); } if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //true: append 追加的方式打开 FileOutputStream fouts = new FileOutputStream(file, false); //将FileOutputStream包装成PrintStream PrintStream ps = new PrintStream(fouts); //输出文件内容 ps.print(content); ps.close(); fouts.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- 接收充电状态和电量的系统广播
- 监控电量和充电状态
- Android中检查、监听电量和充电状态的方法
- Android 广播BroadcastReceiver静态/动态注册,获取系统电量广播【开机广播】,获取网络状态广播,Timer的使用简介
- 根据返回的电量,实现充电状态,充电完成电量显示
- Android 的电池消耗优化 II-监测电量等级和充电状态
- Android开发:优化电池续航-监听电量和充电状态
- 接收系统广播消息之监听手机电量变化
- android监测电池的电量与充电状态
- Android电量和插拔电源状态广播监听
- 电量变化的广播
- 获取当前手机的电池充电状态以及当前的电量
- 【Android Training - Performance】优化电池续航能力[Lesson 1 - 监测设备的电量与充电状态]
- 广播的发送和接收
- 开机关机广播和电量变化广播
- android 电池状态侦听显示 电量 充电动态显示
- android 电池状态侦听显示 电量 充电动态显示
- vc++如何获取笔记本电源电量信息以及充电状态
- template method模式
- Tensorflow 代码学习(1)-GRUCell
- QTreeView的模型的写法注意事项
- DES算法破解需要时间的解析
- 从0开始搭建自己的个人网站教程(二)虚拟主机的选购指南
- 接收充电状态和电量的系统广播
- 如何快速转载csdn的博客
- mysql数据库入门(1)--各种数据库介绍
- MFC中用户自定义消息的实现
- android圆形circleImageview库的使用
- Scala实现冒泡排序、归并排序和快速排序
- 职业规划之虑
- Unreal Engine 4 —— 使用IPlatformFile进行Shader文件在开发团队间的共享
- android.database.sqlite.SQLiteException: no such column: T.XXX (code 1): , while compiling