欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 12:53:58
本文实例讲述了android编程实现添加低电流提醒功能的方法。分享给大家供大家参考,具体如下: 特殊需求,检测电流是否正常。 监听如下广播: intent.a...

本文实例讲述了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] < curr){
            maxindex = i;
          }
        }
        if(cbattnow < 0){ //in the charging
          int min = mcurrent[minindex];
          int max = mcurrent[maxindex];
          if((min < 0 && min < cbattnow) || (min > 0 && min > cbattnow)){ //-1600 < -1400 900 > 800 if true, replace min value.
            mcurrent[minindex] = cbattnow;
          } else if((max < 0 && max < cbattnow) || (max > 0 && max > cbattnow)){ //-1600 < -1400 900 > 800
            mcurrent[maxindex] = cbattnow;
          }
        }
      } else {
        for (int i = 0; i < max_current_count; i++){
          if(mcurrent[i] == 0){
            mcurrent[i] = cbattnow;
            if(i != max_current_count - 1) {
              isfull = false;
            } else {
              isfull = true;
            }
            break;
          }
        }
      }
      //if(isfull && needshowreminddialog(false)){
      if(isfull && needshowreminddialog(true)){
        if(mremiddialog == null){
          mremiddialog = new systemuidialog(mcontext);
          mremiddialog.settitle(r.string.charge_current_warning_title);
          mremiddialog.setpositivebutton(r.string.charge_current_warning_yes, null);
          mremiddialog.setnegativebutton(r.string.charge_current_warning_ignore, new dialoginterface.onclicklistener() {
            @override
            public void onclick(dialoginterface dialog, int which) {
              isignore = true;
            }
          });
        }
        if(debug && mremiddialog.isshowing()){
          mremiddialog.dismiss();
        }
        if(!mremiddialog.isshowing()){
          string message = mcontext.getstring(r.string.charge_current_warning_content);
          if(debug){
            message += "\n";
            for (int i = 0; i < max_current_count; i++){
              message += mcurrent[i];
              message += "  ";
            }
          }
          mremiddialog.setmessage(message);
          mremiddialog.show();
        }
        //clean all save
        cleanallcache();
        mlastremindtime = system.currenttimemillis();
      }
      postdelayed();
    }
  };
  private void postdelayed(){
    mhandler.removecallbacks(lowelectricityremindrunnable);
    mhandler.postdelayed(lowelectricityremindrunnable, low_electricity_remind_delayed);
  }
  private void cleanallcache(){
    for (int i = 0; i < max_current_count; i++){
      mcurrent[i] = 0;
    }
    mlastplugcurrent = 0;
  }
  /**
   * read battery current
   * @return battery current
   */
  private int readcurrent(){
    int cbattnow = 0;
    filereader filereader;
    bufferedreader br;
    try {
      filereader = new filereader(mcurrentnowfile);
      br = new bufferedreader(filereader);
      cbattnow = integer.parseint(br.readline());
      cbattnow = cbattnow / 1000;  //ua to ma
      br.close();
      filereader.close();
      if(debug) {
        slog.e(tag, "last plug current : " + cbattnow);
      }
    } catch (filenotfoundexception e) {
      slog.e(tag, "failure in reading battery current", e);
    } catch (ioexception e) {
      slog.e(tag, "failure in reading battery current", e);
    }
    return cbattnow;
  }
  private boolean needshowreminddialog(boolean filterdata){
    if(isignore){
      return false;
    }
    boolean isneedshow = true;
    if(!filterdata){
      for (int i = 0; i < max_current_count; i++){
        if(mcurrent[i] <= 0){
          isneedshow = false;
          break;
        }
      }
    }
    if(isneedshow){
      long currtime = system.currenttimemillis();
      if(debug){
        slog.e(tag, "mlastremindtime = " + mlastremindtime + "  currtime = " + currtime);
      }
      if(mlastremindtime == 0){
        isneedshow = true;
      } else if(mlastremindtime + remind_interval <= currtime){
        isneedshow = true;
      } else{
        isneedshow = false;
      }
    }
    if(debug){
      slog.e(tag, "need show remind dialog = " + isneedshow);
    }
    return isneedshow;
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android硬件相关操作与应用总结》、《android文件操作技巧汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》、《android视图view技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: