0

I want to display a notification from my application at a specific time say 6.30 am daily. I have successfully done that. Following is the code which executes at specified time. However the code works only if the app is in open state or put to background. It does not work if I reboot the device and don't launch the app, also does not work if I kill the app .

AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
PendingIntent alarmIntent;

alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
alarmIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,6);
calendar.set(Calendar.MINUTE,30);
calendar.set(Calendar.AM_PM,Calendar.AM);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
        alarmIntent);
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

Following is the receiver BroadCast method

@Override
public void onReceive(Context context, Intent intent) {
    Intent service1 = new Intent(context, MyAlarmService.class);
    context.startService(service1);

}

The alarm service class displays a notification.

/...Display notification.../

Pang
  • 8,605
  • 144
  • 77
  • 113
Joyson
  • 1,543
  • 4
  • 27
  • 45
  • Do either of these answer your question? [1](http://stackoverflow.com/questions/4452565/start-app-at-a-specific-time) [2](http://stackoverflow.com/questions/11664442/how-to-launch-my-app-every-day-at-some-particular-time) – Pikamander2 Mar 14 '15 at 06:26
  • @Pikamander2 :I tried the above link.The only problem is if i reboot the device / kill the app ,the event is never triggered.Is there any other approach apart from AlarmManager or am i missing something? – Joyson Mar 14 '15 at 06:29
  • see this for kill problem http://stackoverflow.com/questions/20636330/start-sticky-does-not-work-on-android-kitkat-edit-and-jelly-bean – Hardik Mar 14 '15 at 06:33
  • see this for boot problem http://stackoverflow.com/questions/4562734/android-starting-service-at-boot-time – Hardik Mar 14 '15 at 06:34
  • @Hardik , will it be right solution to start the service at BootTime and keep it running always? – Joyson Mar 14 '15 at 07:18

0 Answers0