Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在特定日期设置AlarmManager_Android_Alarmmanager - Fatal编程技术网

Android 在特定日期设置AlarmManager

Android 在特定日期设置AlarmManager,android,alarmmanager,Android,Alarmmanager,我想在特定的日期祝酒。 我尝试使用以下代码: private void setAlertOnSpesificDate() { Calendar calendar = Calendar.getInstance(); calendar.set(info.eventYear, info.eventMonth, info.eventDayOfMonth, info.eventHour, info.eventMinute, 0);

我想在特定的日期祝酒。 我尝试使用以下代码:

private void setAlertOnSpesificDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(info.eventYear, info.eventMonth, info.eventDayOfMonth,
                info.eventHour, info.eventMinute, 0);
        long timeToSetTheAlarm = calendar.getTimeInMillis();
        AlarmManager alarms = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        Receiver receiver = new Receiver();
        IntentFilter filter = new IntentFilter("ALARM_ACTION");
        registerReceiver(receiver, filter);

        Intent intent = new Intent("ALARM_ACTION");
        intent.putExtra("param", "My scheduled action");
        PendingIntent operation = PendingIntent
                .getBroadcast(this, 0, intent, 0);
        long t = timeToSetTheAlarm - System.currentTimeMillis();
        alarms.set(AlarmManager.RTC_WAKEUP, t, operation);
    }
以及定义如下的接收器:

public class Receiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Toast.makeText(context, intent.getStringExtra("param"),
                    Toast.LENGTH_SHORT).show();
        }

    }
问题是onReceive方法中的Toast会立即启动,而不是在我设置的日期启动。 有什么想法吗

提前感谢!:)


p、 我试着把闹钟放进去。set()“timeToSetTheAlarm”代替了t,但是烤面包片根本没有被点燃。

当用
RTC\u WAKEUP设置闹钟时,它需要的是你想要让闹钟响起来的确切时间,而不是从现在开始的时间。
因此,您不必减去当前时间:

alarms.set(AlarmManager.RTC_WAKEUP, timeToSetTheAlarm, operation);
还要注意的是,从目标API 19中,如果需要精确的报警时间,请使用
setExact
,否则它将优化报警时间以减少唤醒

另一个常见错误是日历使用基于0的月份(因此建议使用Calendar.APRIL之类的常量(即3))