I have tried to set up an alarm that will give notification at a particular time of a particular date just for once.But it is giving notification continuously after the given time.And the notification is not working when the application is closed.But I want to run the notification process on background.Essential permissions have been added to the manifest and broadcast receiver also been used. The code is given below.
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.SEPTEMBER);
//calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.DAY_OF_MONTH, 15);
calendar.set(Calendar.HOUR_OF_DAY, 22);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 05);
//calendar.set(Calendar.AM_PM,Calendar.PM);
final int id = (int) System.currentTimeMillis();
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent= PendingIntent.getBroadcast(MainActivity.this, id, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
// If the alarm has been set, cancel it.
/*if (alarmManager!= null) {
alarmManager.cancel(pendingIntent);
}*/
} //end onCreate
}