Using Alarm Manager at specific time

Abhinav :

I need to trigger a block of code at 11:59 pm IST from the AlarmManager being set.

Can someone show me sample code on how to use an AlarmManager in ِAndroid?

I have been playing around with some code for a few days and it just won't work.

sebasira :

Here's a sample code of using the AlarmManager, you will need to change date and time according to your needs. I put it for today at 23:59

// Pending intent to be fired when alarm occurrs. In this case, open the AlarmActivity
Intent intent = new Intent(getApplicationContext(), AlarmActivity.class);
PendingIntent alarmIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

// Set the alarm for today at 23:59
calendar = Calendar.getInstance(TimeZone.getTimeZone("IST"));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(),
        alarmIntent);

In the given date-time the Intent will be fired so you need to make you logic in the AlarmActivity. You could also change the intent to start a Service or fire a Broadcast message

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409587&siteId=1