Android Alarm Manager

Hi, I studied the alarm manager in android and wondered how to set a specific time for an alarm (or use a notification manager) to leave at a specific time, for example, tomorrow 12pm. The code below sets an alarm for 5 seconds, so to set it to 12 hours, can you do something like 12:00 or something else?

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(); 
+4
source share
2 answers

Java date and time libraries are a huge problem, 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(); 
+12
source
 Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()); 

it should work

0
source

Source: https://habr.com/ru/post/1301400/


All Articles