yoshi24
yoshi24

Reputation: 3177

Service running AsyncTask over and over in onCreate() method

I have a Service that i run when a alarm is sent.

The problem is the service runs ever couple of hours automatically.

I only want it to run once when the alarm is executed.

Here is the code i am using:

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onStart(Intent intent, int startId) {
   loadList service_release = new loadList();
    service_release.execute();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return startId;

}

@Override
public void onCreate(){
    super.onCreate();
        loadList service_release = new loadList();
    service_release.execute();
            }

Someone told me i should move the AsyncTask to execute in onStartCommand(). But i didnt see anything in the docs that will fix my problem by doing this.

What do i need to do for it to run once and dont kepp running over and over throughout a time period after the alarm is set off?

EDIT:

My Alarm is being set up like this..

String alarm = Context.ALARM_SERVICE;

AlarmManager am = (AlarmManager)getSystemService(alarm);

Intent Aintent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, Aintent, 0);

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.FRIDAY, );
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 4 * AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, pi);

Upvotes: 0

Views: 682

Answers (1)

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52946

You have a repeating alarm, that means it will be triggered repeatedly, and launch the service each time it is. If you don't need it to repeat, set a non-repeating alarm, using AlarmManager.set().

Upvotes: 1

Related Questions