0

AlarmManager应该每隔1分钟重复一次,但每1,2,3或4分钟重复一次。设置重复AlarmManager在指定的时间内没有响应

由于应用我扔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); 
} 
} 

由于广播接收器调用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"); 
} 
} 

当这种情况发生的背景,我有一对夫妇在前台运行活动。

+0

您如何测量广播接收器或意图服务中的这个?如果在意向服务中,如果有任何事情导致服务运行很长,则会阻止下一次启动。 –

+0

我测量logcat中的打印时间,从intentservice发送打印。 – manduinca

+0

从接收器来衡量,让我们试着消除潜在的问题来源。如果每分钟拨打一次接收者,问题出在您的服务上。如果不是,那么它与警报。 –

回答

2

从KitKat(API 19)开始,报警器是未加工和批量生产的,通过最大限度地减少设备需要唤醒的次数来延长电池寿命。

the AlarmManager documentationsetRepeating()

注:为API 19日,所有重复报警是不准确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,如上所述每次重新安排时间。 targetSdkVersion早于API 19的传统应用程序将继续保留其所有警报,包括重复警报,并将其视为确切对待。

要获得更高的精度,您需要使用setExact(),并在每次警报唤醒您的应用时重新安排警报。尽管这样做要非常小心,因为设置频繁的警报(例如每1分钟)会显着降低用户的电池寿命。

setExact() documenation:

注:只有报警针对其存在的确切时间交付强大的需求(如闹钟在要求的时间振铃)应该被安排为精确。强烈建议应用程序不要使用精确警报,因为它们会降低操作系统将电池使用量降至最低的能力。

相关问题