简体   繁体   English

Alarmmanager第一次触发但不重复

[英]Alarmmanager fires first time but doesn't repeat

I want an Alarmmanager to fire first time after 2 Seconds and then every 10 Sec. 我希望警报管理器在2秒后第一次触发,然后每10秒触发一次。

It doesn't fire the first time exactly 2 seconds later. 恰好2秒后,它不会第一次启动。 Something between 5 and 10 sec later. 5到10秒后。 And it doesn't repeat at all. 它根本不会重复。

Here is my Code: 这是我的代码:

Alarmmanager: Alarmmanager:

Intent intent = new Intent(this, BackgroundService.class);
final PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 2 * 1000, 10 * 1000, pendingintent);

Manifest: 表现:

<receiver android:process=":remote" android:name=".BackgroundService"/>

BackgroundService.java: BackgroundService.java:

public class BackgroundService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("BackgroundService", "BackgroundService onReceive");
    }
}

Got it: 得到它了:

The IDE says: The 3rd value will be forced up to 60000 (1 Min) to save battery. IDE表示:第三个值将被强制高达60000(1分钟)以节省电池。 But I never waited so long, so it looked like it doesn't even repeates. 但是我没等那么久,所以看起来它甚至没有重复。

Snap is here Snap在这里

am.setRepeating(AlarmManager.RTC_WAKEUP, **cal_alarm.getTimeInMillis()**, 1000*60*5 , pendingIntent); 

I think you are using System.currentTimeMillis() because of which you are having that issue. 我认为您正在使用System.currentTimeMillis(),因此您遇到了该问题。 Try to get the time from you code for which you have set the Alarm. 尝试从设置了警报的代码中获取时间。 The third parameter in the above code is the repeating time. 上面代码中的第三个参数是重复时间。 You can set it to 10 sec. 您可以将其设置为10秒。

this heslp me 这让我心烦

if (android.os.Build.VERSION.SDK_INT >= 19) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            }

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

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