0

I'm trying to schedule a service through AlarmManager with following code:

The onCreate() of MainActivity is as follows:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   /*
    if(isMyServiceRunning(MyService.class)) {
        Toast.makeText(this, "Service already Running", Toast.LENGTH_LONG).show();
    }
    else{
        startService(new Intent(getBaseContext(), MyService.class));
    }
    */
    AlarmManager alarmManager;
    PendingIntent alarmIntent;
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    int hour = 12;
    int minutes = 14;
    alarmManager = (AlarmManager) getSystemService(context.ALARM_SERVICE);
    Intent i = new Intent (getBaseContext(), AlarmReceiver.class);
   // Intent i = new Intent();
   // i.setClass(getBaseContext(),MyService.class);
   // alarmIntent = PendingIntent.getService(getBaseContext(), 0, i, 0);
    alarmIntent = PendingIntent.getBroadcast(getBaseContext(), 0, i, 0);
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}

while the AlarmReceiver is as follows:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
            context.startService(new Intent(context, MyService.class));
    }
}

I've also tried PendingIntent.getService(), as is visible in code.

the service was starting as required without scheduling...please suggest.

or, is it that, since setInexactRepeating is not exact ...in that case how long will i have to wait.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
anant sophia
  • 55
  • 2
  • 12

1 Answers1

0

You have to use

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

instead of

alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
Roman Zhukov
  • 506
  • 5
  • 8
  • checking, no response yet.....can you suggest any way, other than waiting for service to start, to check if alarm has been set – anant sophia May 02 '15 at 08:11
  • see http://stackoverflow.com/questions/6522792/get-list-of-active-pendingintents-in-alarmmanager – Roman Zhukov May 02 '15 at 08:13
  • on repetitive adb shell dumpsys alarm, i could see value of WHEN change from 9m to 4 sec to presently 23h59m, but the service hasn't started.. – anant sophia May 02 '15 at 08:32
  • why are you using getBaseContext()? Try to use your activity as context. – Roman Zhukov May 02 '15 at 08:58
  • my mistake, i did not define the receiver in manifest, now ...able to start both the service and the broadcast receiver. Thanx @Roman Zhukov – anant sophia May 02 '15 at 09:02
  • you should add randomness so request from different devices won't hit the server simultaneously - if you use it with a server – sivi May 04 '15 at 18:57