0

I am building an Android application that sets an alarm based on my application's server time.

By comparing GMT and application server time am trying to create an alarm.

for example: GMT = 10.30 am and server time = 4.30 am, so i need to set alarm after 1hour/2hour from server time.

Example code:

Calendar calender=Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calender.set(Calendar.HOUR,-6); // my server running time is less than 6 hours of GMT time.
calender.set(Calendar.MINUTE,0);
calender.set(Calendar.SECOND,0);

can anyone has idea on how to set alarm
How do I set the alarm?

Ramesh C
  • 43
  • 1
  • 8

1 Answers1

0

From what I understand, here is what you need to do.

  1. Get Calendar object for GMT timezone.
  2. Get current time.
  3. Set current time to your calendar object.
  4. Finally, add one hour to this calendar object.

Here is how you can achieve this:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long currentTime = System.currentTimeMillis();
calendar.setTimeInMillis(currentTime);
calendar.add(Calendar.HOUR, 1);

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
  • if i followed this working as a server time only and set the alarm time,unable to work the alarm. – Ramesh C Aug 24 '16 at 08:32
  • @RameshC: Can you please elaborate **unable to work the alarm.** ? What results do you see? – anacron Aug 24 '16 at 09:06
  • if is reduce server time from GMT time , an alarm suddenly fire,if i do not reduce the server time and add minutes and seconds an alarm is fire as for proper time . thanks for reply..help me.... – Ramesh C Aug 24 '16 at 11:00
  • Are you saying that if you don't set the -6 hours for the HOUR field, it is working fine? I'm sorry, but I'm not able to understand what you're trying to say.. – anacron Aug 24 '16 at 12:18
  • yes,if i don't set the -6 in hours field it is working,but if i add it alarm will fire appln run time it self,(suddendly). – Ramesh C Aug 24 '16 at 12:31
  • Can you login to this [chat room](http://chat.stackoverflow.com/rooms/19132/java-and-android-era) – anacron Aug 24 '16 at 12:35