AlarmManager的setRepeating在指定的时间内没有响应 [英] setRepeating of AlarmManager not respond within the time indicated

查看:597
本文介绍了AlarmManager的setRepeating在指定的时间内没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AlarmManager应该每1分钟重复一次,但应每1、2、3或4分钟重复一次。

AlarmManager should be repeated every 1 minute, but repeated every 1, 2, 3 or 4 minutes.

由于应用程序我抛出了AlarmManager

Since the application I throw 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。

Since BroadcastReceiver call a IntentService.

public class GpsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Intent gps = new Intent(context, GpsIntentService.class);
    context.startService(gps);
}
}

意图服务执行任务

public class GpsIntentService extends IntentService {

public GpsIntentService() {
    super("GpsIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    System.out.println("Intent service ejecutado");
}
}

由于这是在后台发生的,

As this occurs in the background, I have a couple of activities running in the foreground.

推荐答案

从KitKat(API 19)开始,警报不精确并分批保存以节省电池寿命通过最小化设备需要唤醒的次数。

As of KitKat (API 19), alarms are inexacted and batched together to preserve battery life by minimizing the number of times the device needs to wake up.

来自 AlarmManager文档,用于 setRepeating()


注意:从API 19开始,所有重复的警报都是不精确的。如果您的应用程序需要精确的交付时间,则它必须使用一次性精确警报,并如上所述每次重新安排。 targetSdkVersion 早于API 19的旧版应用程序将继续将其所有警报(包括重复警报)视为完全相同。

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

要获得更高的精度,您将需要使用 setExact() ,并在每次闹钟唤醒您的应用时重新安排闹钟。不过,请务必谨慎,因为设置频繁的警报(例如每1分钟一次)会极大地减少用户的电池寿命。

For more precision you will need to use setExact() and reschedule an alarm every time an alarm wakes up your app. Do so very carefully though, as setting frequent alarms (such as every 1 minute) will dramatically decrease your users' battery life.

setExact ()文档:


注意:只有对精确时间交付有强烈需求的警报( (例如在请求的时间响起的闹钟)应准确安排。强烈建议应用程序不要使用不必要的确切警报,因为它们会降低操作系统降低电池使用量的能力。

Note: only alarms for which there is a strong demand for exact-time delivery (such as an alarm clock ringing at the requested time) should be scheduled as exact. Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use.

这篇关于AlarmManager的setRepeating在指定的时间内没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆