Android的报警工作,而是延迟 [英] Android Alarm working, but delayed

查看:162
本文介绍了Android的报警工作,而是延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的闹钟和一切工作,因为它应该,但我发现,报警的发射被延迟。

我想问题可能是系统记录的确切毫秒我设置闹钟,然后烧制在确切毫秒报警。因此,举例来说,如果我做出正确的报警为8:00,但我设置报警10:00:30.225pm,报警将触发在8:00:30.225am

我会如何确保在火警准确的时间吗?我试着用setExact(...),但只是导致我的应用程序崩溃。

在此先感谢!

 公共无效setAlarm(查看视图){    TimePicker timePick =(TimePicker)findViewById(R.id.time_pick);
    台历挂历= Calendar.getInstance();    //设置闹钟根据时间选择器值
    calendar.setTimeInMillis(System.currentTimeMillis的());
    calendar.set(Calendar.HOUR_OF_DAY,timePick.getCurrentHour());
    calendar.set(Calendar.MINUTE,timePick.getCurrentMinute());    setAlarmDisplay(日历); //显示时间EEE,yyyy年MMM d HH:MM AA格式    意向意图=新意图(这一点,AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(在此,0,意图,0);
    alarmManager =(AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
    alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),alarmIntent);    Toast.makeText(view.getContext(),报警设置!,Toast.LENGTH_LONG).show();
}


解决方案

我宁愿发表评论,但我没有50代表没有,所以我不得不提出一个答案。

我认为这个问题是行

  calendar.setTimeInMillis。

您再设置小时和分钟,但一切(秒和毫秒)停留,正是因为它是当运行code。

如果您希望闹钟在完全相同10:30熄灭,设定时间,设定分钟,然后重新设置秒和毫秒为0;

I'm trying to make a simple alarm clock and everything is working as it should, but I'm finding that the firing of the alarm is delayed.

I think the problem may be that the system is recording the exact millisecond I am setting the alarm, and then firing the alarm at that exact millisecond. So for example, if I make an alarm for 8:00am but I set the alarm at 10:00:30.225pm, then the alarm will fire at 8:00:30.225am.

How would I go about making sure the alarm fires exactly on the minute? I tried using setExact(...) but that just caused my app to crash.

Thanks in advance!

public void setAlarm(View view) {

    TimePicker timePick = (TimePicker) findViewById(R.id.time_pick);
    Calendar calendar = Calendar.getInstance();

    //Set alarm based on values in time picker
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, timePick.getCurrentHour());
    calendar.set(Calendar.MINUTE, timePick.getCurrentMinute());

    setAlarmDisplay(calendar); //shows time in EEE, MMM d yyyy hh:mm aa format

    Intent intent = new Intent(this, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

    Toast.makeText(view.getContext(), "Alarm set!", Toast.LENGTH_LONG).show();
}

解决方案

I'd rather comment this but I don't have 50 rep yet so I'll have to submit an answer.

I think the problem is the line

calendar.setTimeInMillis. 

You then set the hour and minute but everything else (seconds and ms) stay exactly as it was when you ran that code.

If you want the alarm to go off at exactly 10:30, set the hours, set the minutes and then reset the seconds and milliseconds to be 0;

这篇关于Android的报警工作,而是延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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