【发布时间】:2016-03-09 20:24:46
【问题描述】:
AlarmManager 应该每 1 分钟重复一次,但每 1、2、3 或 4 分钟重复一次。
自从应用我抛出AlarmManager
public class PacienteApp extends Application {
@Override
public void onCreate() {
AlarmManager gps = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, GpsReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i, 0);
gps.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60, pending);
}
}
由于 BroadcastReceiver 调用了 IntentService。
public class GpsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent gps = new Intent(context, GpsIntentService.class);
context.startService(gps);
}
}
和intentservice执行任务
public class GpsIntentService extends IntentService {
public GpsIntentService() {
super("GpsIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("Intent service ejecutado");
}
}
由于这发生在后台,我有几个活动在前台运行。
【问题讨论】:
-
你是如何衡量这个的——在广播接收器中还是在意图服务中?如果在意图服务中,如果有任何事情导致服务长时间运行,它将阻止下一个服务启动。
-
我在logcat中测量打印时间,打印是从intentservice发送的。
-
改为从接收器测量,让我们尝试消除潜在的问题来源。如果每分钟都呼叫接收者,则问题出在您的服务上。如果不是,那么它与警报有关。
-
还要记住“注意:从 API 19 开始,所有重复的警报都是不准确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性准确的警报,每次都重新安排如上所述。旧版targetSdkVersion 早于 API 19 的应用程序将继续将其所有警报(包括重复警报)视为准确。"
标签: android alarmmanager android-intentservice android-broadcastreceiver