Reputation: 1934
Im try to create a service in my app to update data everyday at 2pm. I want to set a repeating alarm which triggers a service to fetch the data for me. This has nothing to do with UI thread and should work even when the app is closed.
I can't seem to get my service to start.
Here is my code in my activity where I create the alarm
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 20); // HOUR
cal.set(Calendar.MINUTE, 0); // MIN
cal.set(Calendar.SECOND, 0); // SEC
Intent intent = new Intent(Main.this, VenueUpdater.class);
PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
Then my service class
public class VenueUpdater extends Service{
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "Create", Toast.LENGTH_SHORT).show();
Log.i("SERVICE", "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "Destroy", Toast.LENGTH_SHORT).show();
Log.i("SERVICE", "onDestroy");
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
Log.i("SERVICE", "onStart");
}
}
and in my Manifest just before I close my application tag
<service android:enabled="true" android:name="services.VenueUpdater" />
</application>
I've checked some other examples and code where I've used services and the code seems to be fine but still doesn't work. Also I'm wondering if there is a better way to achieve this as the same alarm may end up being created multiple times but there may be a Pending intent Flag I can use to check that it doesn't.
Upvotes: 1
Views: 975
Reputation: 95626
You've set your alarm to go off at 8PM, not 2PM with this:
cal.set(Calendar.HOUR_OF_DAY, 20); // HOUR
If you want to make sure that your alarm isn't scheduled multiple times, you can cancel any previously scheduled alarms before setting the alarm, like this:
alarm.cancel(pintent);
Also, this call to the AlarmManager
:
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
Will set your alarm to go off at 8PM and then every 30 seconds after that. Is that what you want?
EDIT Show how to set the alarm to repeat once per day at 2PM
To schedule the alarm to repeat once per day at 2PM, use this:
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pintent);
Upvotes: 4
Reputation: 5671
1) In Service
subclass use onStartCommad
method to start all service routine. Not onStart/onCreate.
2) Here is example of my AndroidManife:
<service android:name=".service.AlertService" />
3) PendingIntent
will everlap previous one (i.e. overlap alam) if it uses the same requestCode
(the second param of getService
method) and flag PendingIntent.FLAG_UPDATE_CURRENT
is used as last param.
Try it.
Upvotes: 0