服务在onCreate()方法中反复运行AsyncTask

时间:2011-09-27 23:06:46

标签: android android-asynctask android-service

我有一个发送警报时运行的服务。

问题是服务会自动运行几个小时。

我只希望它在警报执行时运行一次。

以下是我正在使用的代码:

@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();
            }

有人告诉我,我应该将AsyncTask移到onStartCommand()中执行。但我没有看到文档中的任何内容可以通过这样做来解决我的问题。

在闹钟响起后的一段时间内,我需要做些什么来运行一次并且不要让kepp一遍又一遍地运行?

编辑:

我的闹钟就像这样设置..

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);

1 个答案:

答案 0 :(得分:1)

您有重复警报,这意味着它将被重复触发 ,并在每次启动时启动该服务。如果您不需要重复,请使用AlarmManager.set()设置非重复闹铃。