热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

Android编程实现添加低电流提醒功能的方法

这篇文章主要介绍了Android编程实现添加低电流提醒功能的方法,涉及Android广播监听及电源监控等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现添加低电流提醒功能的方法。分享给大家供大家参考,具体如下:

特殊需求,检测电流是否正常。

监听如下广播:

Intent.ACTION_BATTERY_CHANGED
plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
if(mLowElectricityRemind == null){
  mLowElectricityRemind = new LowElectricityRemind(BatteryMeterView.this.getContext());
}
mLowElectricityRemind.changePlugType(plugType);

添加LowElectricityRemind类

package com.android.systemui;
import android.content.Context;
import android.content.DialogInterface;
import android.os.BatteryManager;
import android.os.Handler;
import android.util.Slog;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.android.systemui.statusbar.phone.SystemUIDialog;
/**
 * add low electricity remind
 * Created by fanlj on 2017-2-18.
 */
public class LowElectricityRemind {
  private static final String TAG = LowElectricityRemind.class.getSimpleName();
  private static final int LOW_ELECTRICITY_REMIND_DELAYED = 50000;
  private static final long REMIND_INTERVAL = 3 * 1000 * 60; //Three minutes
  private static final int MAX_CURRENT_COUNT = 3;
  private static final boolean DEBUG = true;
  private boolean isFirstInitialize = true;
  private Context mContext;
  private Handler mHandler;
  private int[] mCurrent = new int[MAX_CURRENT_COUNT];
  private File mCurrentNowFile = null;
  private SystemUIDialog mRemidDialog;
  private long mLastPlugCurrent = 0;
  private long mLastRemindTime = 0; //if mRemidDialog is showed, mLastRemindTime != 0
  private boolean isIgnore = false;
  LowElectricityRemind(Context context){
    mCOntext= context;
    mHandler = new Handler();
    mCurrentNowFile = new File("/sys/class/power_supply/battery/current_now");
  }
  public void changePlugType(int type){
    if(DEBUG) {
      Slog.e(TAG, "change plug type to " + type);
    }
    mHandler.removeCallbacks(lowElectricityRemindRunnable);
    if(type == BatteryManager.BATTERY_PLUGGED_AC || (DEBUG && type == BatteryManager.BATTERY_PLUGGED_USB)){
      if(DEBUG) {
        Slog.e(TAG, "start runnable");
      }
      if(isFirstInitialize){
        isFirstInitialize = false;
      }
      mHandler.postDelayed(lowElectricityRemindRunnable, LOW_ELECTRICITY_REMIND_DELAYED);
    } else {
      cleanAllCache();
    }
  }
  private Runnable lowElectricityRemindRunnable = new Runnable() {
    @Override
    public void run() {
      if(!needShowRemindDialog(true)){
        postDelayed();
        return;
      }
      boolean isFull = true;
      int cbattNow = readCurrent();
      if(mLastPlugCurrent == cbattNow){
        postDelayed();
        return;
      }
      mLastPlugCurrent = cbattNow;
      if(mCurrent[MAX_CURRENT_COUNT - 1] != 0){
        int minIndex = 0;
        int maxIndex = 0;
        for (int i = MAX_CURRENT_COUNT; i > 1; i--){
          int curr = mCurrent[i];
          if(mCurrent[minIndex] > curr){
            minIndex = i;
          }
          if(mCurrent[maxIndex]  0 && min > cbattNow)){ //-1600 <-1400 900 > 800 if true, replace min value.
            mCurrent[minIndex] = cbattNow;
          } else if((max <0 && max  0 && max > cbattNow)){ //-1600 <-1400 900 > 800
            mCurrent[maxIndex] = cbattNow;
          }
        }
      } else {
        for (int i = 0; i 
            var cpro_id = "u6885494";

        
        
    
推荐阅读
author-avatar
奇国的雪儿
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有