当前位置:   article > 正文

接收充电状态和电量的系统广播_broadcastreceiver 只有充电才执行

broadcastreceiver 只有充电才执行
  1. public class BroadcastR_PowerListener extends BroadcastReceiver {
  2. private static final String ACTION_POWER_CONNECTED = "android.intent.action.ACTION_POWER_CONNECTED";
  3. private static final String ACTION_POWER_DISCONNECTED = "android.intent.action.ACTION_POWER_DISCONNECTED";
  4. private static final String POWER_LOG_PATH = "Cache";
  5. private static BroadcastReceiver instance = null;
  6. private BroadcastR_PowerListener() {
  7. }
  8. public static void registerPowerListener() {
  9. if (instance == null) {
  10. instance = new BroadcastR_PowerListener();
  11. IntentFilter intentFilter = new IntentFilter();
  12. intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
  13. intentFilter.addAction(ACTION_POWER_CONNECTED);
  14. intentFilter.addAction(ACTION_POWER_DISCONNECTED);
  15. intentFilter.setPriority(Integer.MAX_VALUE);
  16. MyApplication.getInstance().registerReceiver(instance, intentFilter);
  17. }
  18. }
  19. @Override
  20. public void onReceive(Context context, Intent intent) {
  21. if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
  22. //你可以读到充电状态,如果在充电,可以读到是usb还是交流电
  23. // 是否在充电
  24. int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  25. boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
  26. status == BatteryManager.BATTERY_STATUS_FULL;
  27. // 怎么充
  28. int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  29. boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  30. boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  31. String fileName = String.format("uid_%d_power.txt", User.getUid());
  32. int powerLevel = intent.getIntExtra("level", 0);
  33. if (usbCharge) {
  34. write(powerLevel+"", POWER_LOG_PATH, fileName);
  35. }
  36. } else if (intent.getAction().equals(ACTION_POWER_CONNECTED)) {
  37. ErrRpt.debug("powerlistener", "Power Connected.");
  38. } else if (intent.getAction().equals(ACTION_POWER_DISCONNECTED)) {
  39. ErrRpt.debug("powerlistener", "Power DisConnected.");
  40. }
  41. }
  42. private void write(String content, String directory, String fileName) {
  43. try {
  44. File dir = new File(Environment.getExternalStorageDirectory()
  45. + "/" + directory);
  46. File file = new File(dir + "/" + fileName);
  47. if (!dir.exists()) {
  48. dir.mkdirs();
  49. }
  50. if (!file.exists()) {
  51. try {
  52. file.createNewFile();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. //true: append 追加的方式打开
  58. FileOutputStream fouts = new FileOutputStream(file, false);
  59. //将FileOutputStream包装成PrintStream
  60. PrintStream ps = new PrintStream(fouts);
  61. //输出文件内容
  62. ps.print(content);
  63. ps.close();
  64. fouts.close();
  65. } catch (FileNotFoundException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/738320
推荐阅读
相关标签
  

闽ICP备14008679号