Android 如何在任何选定时间的特定日期设置警报?

Android 如何在任何选定时间的特定日期设置警报?,android,datepicker,alarm,timepicker,Android,Datepicker,Alarm,Timepicker,这是我的代码,它可以工作,但我不能设置闹钟,除非是在凌晨12点 我希望用户在一个特定的日期设置一个警报,就像下面给出的那样,但是能够在一天中的任何时间设置警报,而不仅仅是午夜…有人能帮忙吗 Calendar now=Calendar.getInstance pickerDate.init( now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calenda

这是我的代码,它可以工作,但我不能设置闹钟,除非是在凌晨12点 我希望用户在一个特定的日期设置一个警报,就像下面给出的那样,但是能够在一天中的任何时间设置警报,而不仅仅是午夜…有人能帮忙吗

Calendar now=Calendar.getInstance

    pickerDate.init(
            now.get(Calendar.YEAR),
            now.get(Calendar.MONTH),
            now.get(Calendar.DAY_OF_MONTH),
            null);

    pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
    pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
    buttonSetAlarm = (Button)findViewById(R.id.setalarm);
    buttonSetAlarm.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {

            Calendar current = Calendar.getInstance();

            Calendar cal = Calendar.getInstance();
            cal.set(pickerDate.getYear(),
                    pickerDate.getMonth(),
                    pickerDate.getDayOfMonth(),
                    pickerTime.getCurrentHour(),
                    pickerTime.getCurrentMinute(),
                    0);


            if(cal.compareTo(current) <= 0){


                Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
            }else{
                setAlarm(cal);
            }



        }});
}

private void setAlarm(Calendar targetCal){

    String pattern = "MM/dd/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);






    Calendar cal = Calendar.getInstance();
    cal.set(pickerDate.getYear(),
            pickerDate.getMonth(),
            pickerDate.getDayOfMonth(),
            pickerTime.getCurrentHour(),
            pickerTime.getCurrentMinute(),
            0);
    cal.set(Calendar.MILLISECOND,0);



    try {
    Date date12_25 = format.parse("12/25/2017");

    Lebanon.put(date12_25,"Christmas Day");
        for(Date k : Lebanon.keySet()){

            if(cal.getTime().equals(k)){

                String val = (String)Lebanon.get(k);
                info.setText("Your alarm is set for :"+k+", on:  "+val);
            }

        }



    } catch (ParseException e) {
        e.printStackTrace();
    }format.format(new Date());


    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
使用广播接收器

public class MyBroadcastReceiver extends BroadcastReceiver {  
    MediaPlayer mp;  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        mp=MediaPlayer.create(context, R.raw.alrm   );  
        mp.start();  
        Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();  
    }  
} 
启动警报-

//  i = int time from calander.
     int i = 12;  
            Intent intent = new Intent(this, MyBroadcastReceiver.class);  
            PendingIntent pendingIntent = PendingIntent.getBroadcast(  
                                          this.getApplicationContext(), 234324243, intent, 0);  
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);  
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()  
                                          + (i * 1000), pendingIntent);  
            Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();   

你说我不能设置闹钟是什么意思,除非在凌晨12点?你的闹钟只在凌晨12点响吗?如果选择的时间不是凌晨12点,它就不会被设置吗?是的,我必须将时间选择器设置为凌晨12点才能设置闹钟。我想把闹钟定在某个日期,但不管什么时间@MikeM.那么,你是说,忽略日期,如果你不将时间选择器设置为凌晨12:00,你会得到无效的日期/时间吐司?不,没有无效的日期/时间只会发生在日期时间设置早于当前设置的情况下,就像过去一样,问题在这里我认为ifcal.getTime.equalsk,但我真的不知道如何修复它@MikeM.原来你是对的,问题出在时间选择器的分秒上,所以谢谢@MikeM.lol我想我知道你是从哪里复制的,但这对我的计时问题一点帮助都没有