Chintan
Chintan

Reputation: 936

android alarmmanager reset

My application uses below code to set Alaram to execute service on "Daily" basis.

AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit// intent

        Calendar now = Calendar.getInstance();
        now.add(Calendar.SECOND, 20);
        PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, new Random().nextInt(), i,PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentExecuted);

adb shell dumpsys alarm shows me proper details. like it is scheduled to run daily. However when i install .apk to my phone, it is executing repeatedly. it doesnt wait for a day to execute.

I dont know how to reset alarm set in my phone. i uninstalled the app and newly installed app, but it didn't work. can someone tell me what is wrong?

Upvotes: 0

Views: 1128

Answers (2)

Shoshi
Shoshi

Reputation: 2254

i have used this and this is working fine for me. i have used this alarm to trigger after 18 days. and then again after 18 days and so on:

long time = 1555200000L; //18 days = 18*24*60*60*1000 = 1555200000L
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+time, time, pi); 

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

Right now, you are telling it to go off 20 seconds into the minute

now.add(Calendar.SECOND, 20);

You need to add the hour of the day and possible minute

 now.add(Calendar.HOUR, 12) //set your hour here. noon right now

or

now.add(Calendar.HOUR_OF_DAY, 12)

Calendar

Upvotes: 0

Related Questions