How To Get Remaining Time?
Solution 1:
This is not a good way to do a timer,
i would suggest using a CountDownTimer
then on onTick
u get from that time u know the remaining time,
and best way in that case to move that data to the activity is using
intent.putExtra
read more here:
https://developer.android.com/reference/android/os/CountDownTimer.html
https://developer.android.com/reference/android/content/Intent.html
Solution 2:
java.util.Date EndTime; //time from you are calculating remaining
java.util.DateCurrentTime=null; // current timeSimpleDateFormatdateFormat2=newSimpleDateFormat("HH:mm:ss");
CurrentTime = dateFormat2.parse(dateFormat2.format(newjava.util.Date()));
EndTime = dateFormat2.parse(time_in_string);
longremaining_time= EndTime.getTime() - CurrentTime.getTime();//in milliseconds
Solution 3:
First Choose SimpleDateFormat
as SimpleDateFormat("yyyy-MM-dd hh:mm:ss a")
then you need to pass all the required parameters into date format like (year-month-day hour:minute:second AM) i.e (2021-01-13 5:52:00 AM
). Then simple current time and subrrate current time from endtime.
My code:
java.util.Date EndTime; //time from you are calculating remaining
java.util.DateCurrentTime=null; // current timeSimpleDateFormatdateFormat2=newSimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
CurrentTime = dateFormat2.parse(dateFormat2.format(newDate()));
EndTime = dateFormat2.parse(yearSelected + "-" + monthSelected + "-" + (day + 1) + " " + fajrTimeValue + ":00 AM");
longremainingTime= (EndTime.getTime() - CurrentTime.getTime()) / 1000;//in seconds
Log.d(TAG, "startFajrAlert: remainingTime: " + remainingTime);
Intentintent=newIntent(getContext(), FajrReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(getActivity().getApplicationContext(), 234324243, intent, 0);
AlarmManageralarmManager= (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + remainingTime, AlarmManager.INTERVAL_DAY, pendingIntent);
Note: If the result from subtraction comes in negative then it's means you have passed a past time in the endTime.
Solution 4:
Try this:
AlarmManageralarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
LongNexClockTime= alarmManager.getNextAlarmClock().getTriggerTime();
Longnow= System.currentTimeMillis();
LongtimeLeft= NexClockTime - now;
Solution 5:
You have to create a timer and override its onTick method. Then you can update your timeleft
variable in the intervals you want;
Check my complete answer here: https://stackoverflow.com/a/46012948/2351486
Post a Comment for "How To Get Remaining Time?"