I have been trying to figure out how to get the current time in android and I would like to figure how to run a segment of code when the clock hits a certain time. Ultimately what I am trying to do is when the clock is 9:30 pm. Do something or run a piece of code. I have never dealt with time before on android or java.
Asked
Active
Viewed 272 times
0
-
Does your application will be running as service or you need to start @ 9:30? – Libin Apr 16 '14 at 18:46
3 Answers
0
There are various ways you can get the current time in Android such as
Date currentDate = new Date(System.currentTimeMillis());
Regarding running code at a certain time, check out AlarmManager which allows you to schedule a run for your application.
Also, check out Alarm Manager Example
0
try this:
get the current system time as below:
long currentTime = System.currentTimeMillis();
and set task for future time as below:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
Intent serviceIntent = new Intent(this, GPSTrackerService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, serviceIntent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//every 2 min
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),timeSlice, pintent);
for more description refer below link:

Community
- 1
- 1

MohdTausif
- 498
- 4
- 13
-
How would I apply this, I just want to check if the time is 9:30pm or not? – RedShirt Apr 16 '14 at 20:33
-
please check this http://stackoverflow.com/questions/2992882/how-to-set-an-alarm-to-fire-properly-at-fixed-time – MohdTausif Apr 17 '14 at 06:26