My Alarmmanager For Service Update Doesn't Work
Why my AlarmManager for service update doesn't work? this is my code: public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedI
Solution 1:
To start a service every 30 seconds :
Inside onCreate()
:
Here MyService is the name of the Service.
IntentmyService=newIntent(this, MyService.class);
PendingIntentpendingIntent= PendingIntent.getService(
this, 0, myService, 0);
AlarmManageralarmManager= (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 30*1000,pendingIntent);
This'll start your service every 30 seconds. However, if the service is already running, which might be the case once it has been started the first time, from next time onwards the call will directly go to onStartCommand()
method of the Service and not onCreate()
.
This is all that you need to do. However, if you want to make sure that the AlarmManager keeps restarting the service even after a phone is restarted, you'll need to add a BroadcastReceiver for Boot.
Post a Comment for "My Alarmmanager For Service Update Doesn't Work"