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