真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android編程實現(xiàn)添加低電流提醒功能的方法

本文實例講述了Android編程實現(xiàn)添加低電流提醒功能的方法。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)是一家網(wǎng)站設計公司,集創(chuàng)意、互聯(lián)網(wǎng)應用、軟件技術(shù)為一體的創(chuàng)意網(wǎng)站建設服務商,主營產(chǎn)品:響應式網(wǎng)站開發(fā)、品牌網(wǎng)站制作、成都營銷網(wǎng)站建設。我們專注企業(yè)品牌在網(wǎng)站中的整體樹立,網(wǎng)絡互動的體驗,以及在手機等移動端的優(yōu)質(zhì)呈現(xiàn)。做網(wǎng)站、網(wǎng)站制作、移動互聯(lián)產(chǎn)品、網(wǎng)絡運營、VI設計、云產(chǎn)品.運維為核心業(yè)務。為用戶提供一站式解決方案,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞析悅目的作品,網(wǎng)站的價值服務。

特殊需求,檢測電流是否正常。

監(jiān)聽如下廣播:

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相關內(nèi)容感興趣的讀者可查看本站專題:《Android硬件相關操作與應用總結(jié)》、《Android文件操作技巧匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設計有所幫助。


網(wǎng)頁名稱:Android編程實現(xiàn)添加低電流提醒功能的方法
標題鏈接:http://weahome.cn/article/pjgooe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部