简体   繁体   English

警报无法正常触发,有时甚至根本无法触发

[英]Alarm doesn't fire presicely and sometimes not at all

I'm trying to make an alarm that fires at a user-selected time. 我正在尝试使警报在用户选择的时间触发。 I notice however that the alarm never goes off right on the selected minute. 但是我注意到,闹钟永远不会在选定的分钟响起。 Ie if the user selects 8:30 from the TimePicker dialog, the alarm usually fires somewhere within the 8:30 minute and sometimes it fires over a minute late. 即,如果用户从“时间选择器”对话框中选择8:30,则警报通常会在8:30分钟内触发,有时会延迟一分钟触发。 Also, sometimes the alarm doesn't fire at all. 另外,有时警报根本不发出。 The goal is to update the current alarm so that it isn't always creating new ones. 目标是更新当前警报,以便它并不总是创建新警报。 I'm new to the Android alarm service so I was hoping someone could take a look. 我是Android警报服务的新手,所以我希望有人可以看看。 Here's some code: 这是一些代码:

This method is called in my onCreate 在我的onCreate中调用此方法

private void initAlarmService() {
    calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    ComponentName receiver = new ComponentName(getContext(), AlarmReceiver.class);
    pm = getContext().getPackageManager();
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);


    alarmIntent = new Intent(getContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
}

And this method is called when a time is selected: 当选择时间时,将调用此方法:

private void setTime(int hour, int minute) {
    if(sharedPreferences.contains("ALARM_PREF")){
        manager.cancel(pendingIntent);
    }

    sharedPreferences.edit().putString("ALARM_PREF", ""+hour + ":" +minute).apply();

    //set date object time to picker value
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, 0);

    //interval = 1000 * 60 * 60 * 24
    manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTime().getTime(), interval, pendingIntent);

    //This just outputs to a textview
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a");
    time = sdf.format(calendar.getTime().getTime());
    alarmText.setText(time);
    calendar.setTimeInMillis(System.currentTimeMillis());
}

If the behaviour you're describing is seen on an API 19+ device, this note from AlarmManager documentation might explain why: 如果您描述的行为在API 19+设备上可见,则AlarmManager文档中的此注释可能解释了原因:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. 注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统将转移警报,以最大程度地减少唤醒和电池消耗。 There are new APIs to support applications which need strict delivery guarantees; 有一些新的API支持需要严格交付保证的应用程序。 see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). 请参见setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. targetSdkVersion早于API 19的应用程序将继续看到以前的行为,在该行为中,所有警报均在请求时准确传递。

In your case you'll need to switch to using setExact() . 在您的情况下,您需要切换为使用setExact() Unfortunately there is no setExactRepeating() , so you'll have to manually set it again when you receive first alarm. 不幸的是,没有setExactRepeating() ,因此当您收到第一个警报时,您将不得不再次手动设置它。

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

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