简体   繁体   English

AlarmManager & BroadcastReceiver 没有执行或间隔时间错误

[英]AlarmManager & BroadcastReceiver did not execute or in wrong interval times

I tried the sample of AlarmManager,which in https://developer.android.com/training/scheduling/alarms我尝试了 AlarmManager 的示例,它位于https://developer.android.com/training/scheduling/alarms

My MainActivity.java like this:我的 MainActivity.java 是这样的:

    alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, BroadcastReciever.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    // call every 5mins
    alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
            1000 * 60 * 5L, alarmIntent);

My BroadcastReciever.java like this:我的 BroadcastReciever.java 是这样的:

public class BroadcastReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Execute time:" + Calendar.getInstance().getTime());
    }
}

I hope it output something like "Execute time:Thu Apr 14 17:33:45 GMT+09:00 2022" in every 5mins I installed the app in my physical device and restart the app in my physical device.(device still connect to the PC with an USB cable.)Android studio catch these log below:我希望它每 5 分钟 output 类似于“执行时间:4 月 14 日星期四 17:33:45 GMT+09:00 2022” 我在我的物理设备中安装了该应用程序并在我的物理设备中重新启动了该应用程序。(设备仍然连接到带有 USB 电缆的 PC。)Android Studio 在下面捕获这些日志:

2022-04-14 17:33:45.009 13580-13580/? 2022-04-14 17:33:45.009 13580-13580/? I/com.example.ss: Late-enabling -Xcheck:jni I/com.example.ss:后期启用 -Xcheck:jni

2022-04-14 17:33:45.050 13580-13580/? 2022-04-14 17:33:45.050 13580-13580/? E/com.example.ss: Unknown bits set in runtime_flags: 0x8000 E/com.example.ss:runtime_flags 中设置的未知位:0x8000

2022-04-14 17:33:45.611 13580-13580/com.example.ss I/Perf: Connecting to perf service. 2022-04-14 17:33:45.611 13580-13580/com.example.ss I/Perf:连接到 perf 服务。

2022-04-14 17:33:45.671 13580-13580/com.example.ss I/System.out: Execute time:Thu Apr 14 17:33:45 GMT+09:00 2022 2022-04-14 17:33:45.671 13580-13580/com.example.ss I/System.out:执行时间:2022 年 4 月 14 日星期四 17:33:45 GMT+09:00

The first problem is that, it's not output in every 5mins.第一个问题是,它不是每 5 分钟 output。 The second problem is that, sometimes it was output something like these (not in every 5mins)第二个问题是,有时它是 output 这样的东西(不是每 5 分钟)

2022-04-14 11:56:09.145... 2022-04-14 11:56:09.145...

2022-04-14 11:56:09.150... 2022-04-14 11:56:09.150...

2022-04-14 11:56:09.154... 2022-04-14 11:56:09.154...

I need to make a app, which make a new logfile at 0:00 every day.so I try the alarmmanager & broadcastReciever.我需要制作一个应用程序,每天 0:00 创建一个新的日志文件。所以我尝试了 alarmmanager 和 broadcastReciever。 I need to make sure the logfile only be created one time,(in second case may create three times I guess)我需要确保日志文件只创建一次,(我猜在第二种情况下可能会创建三次)

There are dozens of similar questions on Stackoverflow. Stackoverflow 上有许多类似的问题。 You really should search before you create a new question.在创建新问题之前,您确实应该进行搜索。

Basically, setRepeating() is inexact.基本上, setRepeating()是不准确的。 In order to conserve battery power, Android will change the actual time the alarm triggers.为了节省电池电量,Android 将更改警报触发的实际时间。 You have no control over that.你无法控制它。 If you need exact timing, you should use something like setExact() instead.如果您需要精确的时间,您应该改用setExact()之类的东西。 This is not a repeating alarm, so if you need a repeating alarm you should do something like this: when the alarm gets triggered, do whatever and then set the next alarm.这不是一个重复闹钟,所以如果你需要一个重复闹钟,你应该这样做:当闹钟被触发时,做任何事情然后设置下一个闹钟。

You wrote:你写了:

I need to make a app, which make a new logfile at 0:00 every day.so I try the alarmmanager & broadcastReciever.我需要制作一个应用程序,每天 0:00 创建一个新的日志文件。所以我尝试了 alarmmanager 和 broadcastReciever。 I need to make sure the logfile only be created one time,(in second case may create three times I guess)我需要确保日志文件只创建一次,(我猜在第二种情况下可能会创建三次)

so you should schedule an alarm with setExact() for 00:00 of the following day.所以你应该在第二天的 00:00 使用setExact()安排一个闹钟。 When that alarm triggers, rotate your logfiles and then set an alarm for 00:00 of the following day.当该警报触发时,轮换您的日志文件,然后在第二天的 00:00 设置警报。 This will ensure that your alarm goes off at the correct time and that it only triggers once per day.这将确保您的闹钟在正确的时间响起,并且每天只触发一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM