【发布时间】:2011-09-27 23:06:46
【问题描述】:
我有一个在发送警报时运行的服务。
问题是服务会自动运行几个小时。
我只希望它在警报执行时运行一次。
这是我正在使用的代码:
@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() 中执行。但是我在文档中没有看到任何可以通过这样做来解决我的问题的内容。
我需要做什么才能让它运行一次并且不要在警报响起后的整个时间段内一遍又一遍地运行?
编辑:
我的闹钟是这样设置的..
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被一遍又一遍地运行,那么这表明Service正在一遍又一遍地启动。在不知道“警报”是如何设置/触发的(以及您的AsyncTask的样子)的情况下,很难提供帮助。
标签: android android-asynctask android-service