I have a code for creating an alarmManager
main activity
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 41);
c.set(Calendar.SECOND, 0);
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC,c.getTimeInMillis(), pendingIntent.getBroadcast(this, 0, intentAlarm, pendingIntent.FLAG_ONE_SHOT));
In the AlarmReceive
class I have:
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();
intent = new Intent(context, firstservice.class);
context.startService(intent);
}
In service firstservice
I did some operation. The problem is that the alarm is calling the broadcast receiver on time but after that when I open the app it also call the broadcast receiver and this mean that the service is worked again.
I also had this problem. After copple of hours, I could point out the problem with this solution. In brief I have used intent extras to notify the alarm time.
in the manifest.xml, register the receiver.
This is how I set the alarm manager
This is the OnReceive() method of my DayAlarmReceiver class
Happy coding :)
Try to add an extra to your intent and check it to distinguish your scheduled alarms from any other broadcast your receiver is served