gpt4 book ai didi

android - 警报管理器在特定的给定时间间隔内不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:09 25 4
gpt4 key购买 nike

您好,我在 3 分钟的特定时间间隔内使用警报管理器,然后我开始监控。它有时有效,突然我注意到有不规则的时间间隔,这是不正确的!您可以在附加日志中看到 “20-Jul-2016 12:22:03 pm” 时间发生变化!我连上手机关屏监控了!每 3 分钟,我访问服务器并获得响应 1。但是有一次,访问服务器需要 5 分钟!为什么会出现这个奇怪的问题?

这是代码。

 public void startAt3() {
Intent alarmIntent = new Intent(ActivityTracking.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ActivityTracking.this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/* Set the alarm */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
/* Repeating on every 3 minute interval */
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
180000L, pendingIntent);
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Log.e("alarm",mydate);

}

报警接收器:

   public class AlarmReceiver extends WakefulBroadcastReceiver    {//AlarmReceiver class
@Override
public void onReceive(Context context, Intent intent) {//onReceive method
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
Log.e("alarm",mydate);
Intent service = new Intent(context, SimpleWakefulService.class);//intent to call another class
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, service);//service started
}

简单唤醒服务:

    public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");//instantiates simpleWakefulService

}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("simpleWakeful","simpleWakeful");
serviceCall(this); //here is downloadTaskMethod called and getting response as 1.
}

enter image description here

最佳答案

不要使用setInexactRepeating。顾名思义,这不会安排闹钟在准确的时间响起。

你可以这样使用:

public void scheduleSingleAlarm(Context context) {
Intent intent = new Intent(context, NotificationReceiver.class);
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(context,
SINGLE_ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

Calendar futureDate = Calendar.getInstance();
futureDate.add(Calendar.MINUTE, 3);

setSingleExactAlarm(futureDate.getTime().getTime(), pendingUpdateIntent);
}

@SuppressLint("NewApi")
private void setSingleExactAlarm(long time, PendingIntent pIntent) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
}
}

当您收到闹钟的来电时,安排另一个闹钟在三分钟后响起。我已经写了关于这个的博客 here

关于android - 警报管理器在特定的给定时间间隔内不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com