我正确启动Alarm Manager的时间

时间:2016-11-11 10:58:18

标签: android broadcastreceiver alarmmanager

我遇到了Alarm Manager的问题。我试图在特定时间设置启动闹钟,但在设置时间后2秒钟内开始闹钟。 这是我设置闹钟的代码:

//setting alarm by clicking "add alarm"
        addAlarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //initializing alert dialog with time picker
                final AlertDialog.Builder alert = new AlertDialog.Builder(Events.this);
                final TimePicker timer = new TimePicker(Events.this);
                alert.setMessage(R.string.choose_time);
                alert.setTitle(R.string.set_alarm);
                alert.setView(timer);
                alert.setPositiveButton("Apply", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //getting set time
                        int hour = timer.getHour();
                        int minute = timer.getMinute();
                        //getting alarm service
                        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                        //creating intent for switching to AlarmReceiver.class
                        Intent intent = new Intent(Events.this, AlarmReceiver.class);
                        SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
                        //getting full date with hours and minutes
                        String alarmTime = selectedDate + " " + hour + ":" + minute;
                        //parsing date
                        try {
                            alarmDate = formatter.parse(alarmTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        //creating pending intent
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(Events.this,
                                8452, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        //setting alarm
                        alarmManager.set(RTC_WAKEUP,alarmDate.getTime(), pendingIntent);
                        //info about time set
                        Snackbar.make(bottomRL, "Alarm set in " + hour + ":" + minute,
                                Snackbar.LENGTH_LONG).show();
                    }
                });
                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                });
                alert.show();
            }
        });

这是我的接收者:

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, R.string.alarm_message, Toast.LENGTH_LONG).show();
            Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(2000);
    }
}

有人可以帮我解决问题,请问?

1 个答案:

答案 0 :(得分:0)

这是因为自API 19以来警报传递不准确,请参阅here。操作系统会将警报转换为同时触发多个警报,以节省电池电量。如果您确实要将闹钟设置为精确到秒,则可以使用alarmManager.setExact()