Android编程实现添加低电流提醒功能的方法
本文实例讲述了Android编程实现添加低电流提醒功能的方法。分享给大家供大家参考,具体如下:
特殊需求,检测电流是否正常。
监听如下广播:
Intent.ACTION_BATTERY_CHANGED
plugType=intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
if(mLowElectricityRemind==null){
mLowElectricityRemind=newLowElectricityRemind(BatteryMeterView.this.getContext());
}
mLowElectricityRemind.changePlugType(plugType);
添加LowElectricityRemind类
packagecom.android.systemui;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.os.BatteryManager;
importandroid.os.Handler;
importandroid.util.Slog;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importcom.android.systemui.statusbar.phone.SystemUIDialog;
/**
*addlowelectricityremind
*Createdbyfanljon2017-2-18.
*/
publicclassLowElectricityRemind{
privatestaticfinalStringTAG=LowElectricityRemind.class.getSimpleName();
privatestaticfinalintLOW_ELECTRICITY_REMIND_DELAYED=50000;
privatestaticfinallongREMIND_INTERVAL=3*1000*60;//Threeminutes
privatestaticfinalintMAX_CURRENT_COUNT=3;
privatestaticfinalbooleanDEBUG=true;
privatebooleanisFirstInitialize=true;
privateContextmContext;
privateHandlermHandler;
privateint[]mCurrent=newint[MAX_CURRENT_COUNT];
privateFilemCurrentNowFile=null;
privateSystemUIDialogmRemidDialog;
privatelongmLastPlugCurrent=0;
privatelongmLastRemindTime=0;//ifmRemidDialogisshowed,mLastRemindTime!=0
privatebooleanisIgnore=false;
LowElectricityRemind(Contextcontext){
mContext=context;
mHandler=newHandler();
mCurrentNowFile=newFile("/sys/class/power_supply/battery/current_now");
}
publicvoidchangePlugType(inttype){
if(DEBUG){
Slog.e(TAG,"changeplugtypeto"+type);
}
mHandler.removeCallbacks(lowElectricityRemindRunnable);
if(type==BatteryManager.BATTERY_PLUGGED_AC||(DEBUG&&type==BatteryManager.BATTERY_PLUGGED_USB)){
if(DEBUG){
Slog.e(TAG,"startrunnable");
}
if(isFirstInitialize){
isFirstInitialize=false;
}
mHandler.postDelayed(lowElectricityRemindRunnable,LOW_ELECTRICITY_REMIND_DELAYED);
}else{
cleanAllCache();
}
}
privateRunnablelowElectricityRemindRunnable=newRunnable(){
@Override
publicvoidrun(){
if(!needShowRemindDialog(true)){
postDelayed();
return;
}
booleanisFull=true;
intcbattNow=readCurrent();
if(mLastPlugCurrent==cbattNow){
postDelayed();
return;
}
mLastPlugCurrent=cbattNow;
if(mCurrent[MAX_CURRENT_COUNT-1]!=0){
intminIndex=0;
intmaxIndex=0;
for(inti=MAX_CURRENT_COUNT;i>1;i--){
intcurr=mCurrent[i];
if(mCurrent[minIndex]>curr){
minIndex=i;
}
if(mCurrent[maxIndex]0&&min>cbattNow)){//-1600<-1400900>800iftrue,replaceminvalue.
mCurrent[minIndex]=cbattNow;
}elseif((max<0&&max0&&max>cbattNow)){//-1600<-1400900>800
mCurrent[maxIndex]=cbattNow;
}
}
}else{
for(inti=0;i
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android硬件相关操作与应用总结》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。