This article describes the method of adding low current reminder function for Android programming. Share it for your reference, as follows:
Special requirements, check whether the current is normal.
Listen to the following broadcast:
Intent.ACTION_BATTERY_CHANGED plugType = (BatteryManager.EXTRA_PLUGGED, 0); if(mLowElectricityRemind == null){ mLowElectricityRemind = new LowElectricityRemind(()); } (plugType);
Add LowElectricityRemind class
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * add low electricity remind * Created by fanlj on 2017-2-18. */ public class LowElectricityRemind { private static final String TAG = (); 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) { (TAG, "change plug type to " + type); } (lowElectricityRemindRunnable); if(type == BatteryManager.BATTERY_PLUGGED_AC || (DEBUG && type == BatteryManager.BATTERY_PLUGGED_USB)){ if(DEBUG) { (TAG, "start runnable"); } if(isFirstInitialize){ isFirstInitialize = false; } (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); (.charge_current_warning_title); (.charge_current_warning_yes, null); (.charge_current_warning_ignore, new () { @Override public void onClick(DialogInterface dialog, int which) { isIgnore = true; } }); } if(DEBUG && ()){ (); } if(!()){ String message = (.charge_current_warning_content); if(DEBUG){ message += "\n"; for (int i = 0; i < MAX_CURRENT_COUNT; i++){ message += mCurrent[i]; message += " "; } } (message); (); } //clean all save cleanAllCache(); mLastRemindTime = (); } postDelayed(); } }; private void postDelayed(){ (lowElectricityRemindRunnable); (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 = (()); cbattNow = cbattNow / 1000; //uA to mA (); (); if(DEBUG) { (TAG, "last plug current : " + cbattNow); } } catch (FileNotFoundException e) { (TAG, "Failure in reading battery current", e); } catch (IOException 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 = (); if(DEBUG){ (TAG, "mLastRemindTime = " + mLastRemindTime + " currTime = " + currTime); } if(mLastRemindTime == 0){ isNeedShow = true; } else if(mLastRemindTime + REMIND_INTERVAL <= currTime){ isNeedShow = true; } else{ isNeedShow = false; } } if(DEBUG){ (TAG, "need show remind dialog = " + isNeedShow); } return isNeedShow; } }
For more information about Android related content, please check out the topic of this site:Android hardware-related operations and application summary》、《Android file operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.