Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android alarm manager

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();
like image 588
SamB09 Avatar asked Dec 17 '22 02:12

SamB09


1 Answers

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();
like image 172
emmby Avatar answered Dec 30 '22 20:12

emmby