如何在任何选定时间的特定日期设置闹钟?

How to set an alarm at a specific date at any chosen time?

所以这是我的代码,它可以工作,但我不能设置闹钟,除非在 12:00 AM 我希望用户在特定日期设置闹钟,如下所示,但能够在一天中的任何时间设置,而不仅仅是午夜...有人可以帮忙吗?

日历现在 = 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);
}

使用 BroadcastReceiver。

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();