Tuesday, 18 June 2019

Device's Estimated Battery Time calculation

How to Calculate Android Estimated Battery Remaining Time. 


within your AppCompatActivity

need to do register Battery change event listener with BroadcastReceiver like  
this.registerReceiver(this.mBatInfoReceiver, 
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Now you need to declare with Activity or where you need BroadcastReceiver 
int expectedlevel;
boolean isFirstAllow = true;
int second;

String strFirstTime,strSecondTime;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override    public void onReceive(Context ctxt, Intent intent) {
        
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);

        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Calendar c = Calendar.getInstance();

        if(isFirstAllow) {

            String formattedDate = format.format(c.getTime());
            strFirstTime  = formattedDate;

            expectedlevel = level - 1;
            isFirstAllow = false;
            second = new Time(System.currentTimeMillis()).getSeconds();
        }

        if(!isFirstAllow && level == expectedlevel){

            strSecondTime  = format.format(c.getTime());

            Date d1 = null;
            Date d2 = null;

            try {
                d1 = format.parse(strFirstTime);
                d2 = format.parse(strSecondTime);

                //in milliseconds               
               long diff = d2.getTime() - d1.getTime();

                long diffSeconds = diff / 1000 % 60;
                long diffMinutes = diff / (60 * 1000) % 60;
                long diffHours = diff / (60 * 60 * 1000) % 24;
                long diffDays = diff / (24 * 60 * 60 * 1000);

                System.out.print(diffDays + " days, ");
                System.out.print(diffHours + " hours, ");
                System.out.print(diffMinutes + " minutes, ");
                System.out.print(diffSeconds + " seconds.");

       // Now again we need percentage * difference time  = total long time value.
                long totalMinues =  diffMinutes * expectedlevel;
                long totalSecond  = diffSeconds * expectedlevel;
                long converttoMinutes = totalSecond/60;
                long addedAllMinues = totalMinues + converttoMinutes;

                // Now convert total minitues to millisecond;                
                 long AllMillisond = addedAllMinues * 60000;
     // Now Again convert this total Millisond to hours,minutes, second
                // for Battery Hours;
                long FinaldiffSeconds = AllMillisond / 1000 % 60;
                long FinaldiffMinutes = AllMillisond / (60 * 1000) % 60;
                long FinaldiffHours = AllMillisond / (60 * 60 * 1000) % 24;
                long FinaldiffDays = AllMillisond / (24 * 60 * 60 * 1000);

                System.out.print(FinaldiffDays + " days, ");
                System.out.print(FinaldiffHours + " hours, ");
                System.out.print(FinaldiffMinutes + " minutes, ");
                System.out.print(FinaldiffSeconds + " seconds.");

new AlertDialog.Builder(mcontext)
.setTitle("Battery Remaning Time") .setMessage(FinaldiffDays + " days, "+""+FinaldiffHours + " hours," + ""+""+FinaldiffMinutes + " minutes, "+FinaldiffSeconds + " seconds.") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Continue with delete operation } }) .setNegativeButton(android.R.string.no, null) .setIcon(android.R.drawable.ic_dialog_alert) .show();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
};

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...