views:

721

answers:

1

Hi i've been researching the alarm manager in android and was wondering if there how to set a specific time for an alarm (or could use notification manager)to go off at a specific time for example 12pm tomorrow . The code below sets the alarm for 5 seconds from now so to set it for something like 12 pm could you do something like 12:00:00 or something?

 Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
+3  A: 

Java's Date and Time libraries are a huge hassle, but this should give you an idea:

// the time now
Calendar calendar = Calendar.newInstance();

// noon tomorrow
calendar.add(Calendar.DAY_OF_YEAR, 1);
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Mike
Thanks alot i'll try and work with that later.
SamB09
No problem. Don't forget you can upvote and accept responses :) That will keep people motivated to answer your questions
Mike
I tried it and it works thanks alot.
SamB09
glad to hear it!
Mike
same question Mike BUT I would like to set the alarm at 12 pm New-York Time, whatever the country my user is ! I need to play with TimeZones, isn't it ? How would you do this ? So I want all my users accross the globe to receive the alarm (if they did opt in of course) at the exact same time ...
Hubert
Hi Hubert. Check out the `Calendar(TimeZone,Locale)` constructor. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#Calendar(java.util.TimeZone,%20java.util.Locale)
Mike
Yes, works fine with this : TimeZone tz = TimeZone.getTimeZone("America/New_York"); Calendar calNewYork = new GregorianCalendar(tz);
Hubert