如何为 android 中的特定日期设置闹钟?

How to set alarm for particular date in android?

您好,我需要使用时间为特定日期设置闹钟 picker.When 我给了静态输入,闹钟设置不正确并且响铃。 例如:我正在提供静态输入(带有日期、月份和年份),但它没有响铃。这是我的代码。

  //button click
private OnClickListener reminderEventListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        openTimePickerDialog(false);
    }
  private void openTimePickerDialog(boolean is24r) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,1);//month
        calendar.set(Calendar.YEAR,2015);//year
        calendar.set(Calendar.DAY_OF_MONTH,12);//day
         timePickerDialog = new TimePickerDialog(
                EventDetail.this, 
                onTimeSetListener, 
                calendar.get(Calendar.HOUR_OF_DAY), 
                calendar.get(Calendar.MINUTE), 
                is24r);
        timePickerDialog.setTitle(eventDates+" "+days+","+eventYear);  

        timePickerDialog.show();
    }
    OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener(){

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();
            calSet.set(Calendar.MONTH,1);//month
            calSet.set(Calendar.YEAR,2015);//year
            calSet.set(Calendar.DAY_OF_MONTH,12);//day
            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if(calSet.compareTo(calNow) <= 0){
                //Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);
            }

            setAlarm(calSet);
        }

        private void setAlarm(Calendar targetCal) {
            // TODO Auto-generated method stub
            Log.e("Give input", "12/01/2015");
            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

        }};
};
AlarmReceiver:

 public class AlarmReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     NotificationManager manager =    (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis());
        noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", sender);
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        manager.notify(R.string.app_name, noti); 

        //intent to call the activity which shows on ringing
        Intent myIntent = new Intent(context, EventDetail.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);

        //display that alarm is ringing
        Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show();
        Log.e("Alarm ringing", "alarm ringing"); 

}

   I set particular time and tested.alarm is not triggering.
  I have given permission in manifest too.
   <receiver android:name=".AlarmReceiver" android:process=":remote" />

尝试把MONTH设为0,不是1,会响,MONTHS都是从0开始编号的,不知道为什么会出现这个异常。

我也不知道以毫秒为单位的时间是如何工作的。 也试试看:

long time = calSet.getTime().getTime();

然后把时间放到alarmManager.set().