Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Alarmmanager to start a service at specific time

I have searched a lot of places but couldnt find a clean sequential explanation of how to start a service (or if thats not possible then an activity) at a specific time daily using the AlarmManager??

I want to register several such alarms and triggering them should result in a service to be started. I'll be having a small piece of code in the service which can then execute and i can finish the service for good....

Calendar cal = Calendar.getInstance(); Calendar cur_cal = Calendar.getInstance(); cur_cal.setTimeInMillis(System.currentTimeMillis()); Date date = new Date(cur_cal.get(Calendar.YEAR), cur_cal.get(Calendar.MONTH), cur_cal.get(Calendar.DATE), 16, 45); cal.setTime(date); Intent intent = new Intent(ProfileList.this, ActivateOnTime.class); intent.putExtra("profile_id", 2); PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0); AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); System.out.println("The alarm set!!"); 

i tried this code to activate the alarm at 4.45... but its not firing the service... do i have to keep the process running?? M i doing anything wrong???

One more thing, my service gets perfectly executed in case i use the following code:

long firstTime = SystemClock.elapsedRealtime(); alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent); 
like image 918
JaVadid Avatar asked Jun 16 '10 09:06

JaVadid


People also ask

How to start a service using AlarmManager in android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 4 – Create a java class (MyAlarmService.

How to set alarm at specific time in android?

In the main activity, AndroidTimeActivity, the main code to start Alarm is in setAlarm(Calendar targetCal) method. Retrieve AlarmManager by through getSystemService(Context. ALARM_SERVICE). And set our alarm with alarm type(RTC_WAKEUP), trigger time, and pendingIntent.


1 Answers

HI friends, After a lot of researching and with reference from "Pentium10"'s question on the same topic i managed to get it working. Though i still cant understand why the "date" concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly.

Calendar cur_cal = new GregorianCalendar(); cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar  Calendar cal = new GregorianCalendar(); cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR)); cal.set(Calendar.HOUR_OF_DAY, 18); cal.set(Calendar.MINUTE, 32); cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND)); cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND)); cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE)); cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH)); Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class); PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0); AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 
like image 157
JaVadid Avatar answered Oct 10 '22 17:10

JaVadid