Reputation: 4741
I have to trigger a local notification at 5:30AM and that is working. But the problem is the notification is also getting triggered whenever I open my application. Here is my code in my MainActivity
:
in my onCreate()
of MainActivity
:
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 5);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Here is my MyStartServiceReceiver
:
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("AlarmReceiver", "Started");
Intent intent1 = new Intent(context, AlarmService.class);
intent1.setData(Uri.parse("custom://" + System.currentTimeMillis()));
context.startService(intent1);
}
}
Why is it triggering everytime I am opening my application?
Upvotes: 0
Views: 143
Reputation: 3009
You are setting alarm current date 5:05AM, if it is already passed alarm will be triggered immediately. To stop it do like this.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 5);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
if (System.currentTimeMillis() > calendar.getTimeInMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
What we are doing here is, if time already passed we are adding date by +1 so it will start from next date and we are cancelling previous set alarm.
Upvotes: 2
Reputation: 170
Try this
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
if(calendar.getTime().after(new Date())){
calendar.set(Calendar.Date,1);
}
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Upvotes: 0
Reputation: 46
Because you create
Calendar.getInstance()
for example at 9AM, but set time 5:30AM in the past. So when you use
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
it has already happened.
You can use this
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
Upvotes: 2