2011-09-27 38 views
0

我有一個當發送警報時運行的服務。服務在onCreate()方法中反覆運行AsyncTask

問題是服務會自動運行幾個小時。

我只希望它在執行警報時運行一次。

這裏是我使用的代碼:

@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); 
+0

我不知道你爲什麼要在'Service'中使用'AsyncTask''''AsyncTask'主要是爲了在UI上下文中工作而設計的。 'onStart(...)'也被棄用,你應該使用'onStartCommand(...)'來代替。但是,除此之外,您還沒有提供足夠的信息 - 如果「AsyncTask」反覆運行,則表明「服務」反覆啓動。不知道如何設置/觸發「警報」(也許你的'AsyncTask'看起來像),這很難提供幫助。 – Squonk

+0

查看我的編輯 – yoshi24

回答

1

你有重複報警,這意味着它會被重複觸發,每一次推出的服務也是。如果您不需要重複該操作,請使用AlarmManager.set()設置非重複報警。

+0

我希望它每4天重複一次 – yoshi24

+0

也許你有一些舊的警報。運行'adb shell dumpsys alarm'來檢查註冊的內容。另外,你的setRepeating()調用看起來不正確:第一個參數是'triggerAtTime',第二個'interval',你有什麼會觸發第一個報警_after_4天,之後每天重複。 –