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

作者:extfff 时间:2023-03-16 19:31:29 

本文实例讲述了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程序设计有所帮助。

来源:http://www.cnblogs.com/vendor/p/6437963.html

标签:Android,提醒功能
0
投稿

猜你喜欢

  • Android自定义密码输入框和数字键盘

    2022-02-04 11:14:00
  • 高并发下restTemplate的错误分析方式

    2023-08-23 12:58:24
  • Android实现蓝牙串口通讯

    2023-03-19 09:22:21
  • C++实现的链表类实例

    2023-07-04 08:36:36
  • 使用itextpdf操作pdf的实例讲解

    2022-11-16 00:22:43
  • IDEA集成JProfiler11可视化工具的详细流程(安装、集成、测试)

    2021-12-12 04:05:35
  • WebView的几个常见功能使用方法

    2023-01-25 13:54:48
  • Android开发之ProgressBar字体随着进度条的加载而滚动

    2023-12-28 03:06:53
  • C#简单实现显示中文格式星期几的方法

    2021-09-08 12:27:05
  • Android数字华容道小游戏开发

    2023-10-06 22:31:35
  • Activiti开发环境的配置

    2021-07-31 21:57:51
  • C#解决多IfElse判断语句和Switch语句问题的方法分享

    2023-04-06 18:36:22
  • 使用栈的迷宫算法java版代码

    2022-03-07 12:47:16
  • Java日期操作方法工具类实例【包含日期比较大小,相加减,判断,验证,获取年份等】

    2023-11-25 12:14:40
  • C# 10个常用特性汇总

    2023-03-22 01:04:13
  • 详解Spring-bean的循环依赖以及解决方式

    2023-08-18 18:30:38
  • 高斯混合模型与EM算法图文详解

    2022-10-02 12:05:02
  • C#字符串String及字符Char的相关方法

    2023-10-26 03:05:37
  • Flutter有无状态类与State及生命周期详细介绍

    2022-12-27 15:48:53
  • Spring Security OAuth过期的解决方法

    2023-05-26 22:30:01
  • asp之家 软件编程 m.aspxhome.com