Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager Android Every Day

I'm trying to make a Schedule.

It should run every day at 1pm or 2pm...

At the moment I can only make it run Every 10Sec or 10min...

Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class); pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 10); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);  Toast.makeText(AndroidAlarmService.this, "Start Alarm", Toast.LENGTH_LONG).show(); 

Thanks

like image 981
Paul Avatar asked Dec 30 '10 12:12

Paul


People also ask

Is AlarmManager deprecated?

IntentService + WakefulBroadcastReceiver + AlarmManager are deprecated with API 26 (Android 8.0 Oreo).

How do I set an alarm for multiple days on Android?

Calendar object is used to store one specific date. Therefore, in order to get set alarms for several days, you need to set each of the alarms separately. For this reason you may create separate Calendar objects or reuse one by changing the time. However, you have same receiver class for both alarms.


1 Answers

This code will run the Intent each day on 1 PM or 2 PM

Calendar calendar = Calendar.getInstance();  calendar.set(Calendar.HOUR_OF_DAY, 13); // For 1 PM or 2 PM calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); PendingIntent pi = PendingIntent.getService(context, 0,             new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),                                 AlarmManager.INTERVAL_DAY, pi); 
like image 174
mcanti Avatar answered Sep 22 '22 17:09

mcanti