我怎样才能制作这个闹钟应用程序。能够在一天中的任何时间和准确的时间设置闹钟?

How can I make this Alarm Clock app. to be able to set alarm at any time of the day and exactly at that time?

我是 android 开发的新手,对 java 知之甚少,我在互联网上看到了这个闹钟应用程序教程。应用程序有问题。它不能将时间设置在 phone 当前时间之前,并且不会在确切时间开始。如何制作此应用程序,使其可以在 24 小时内并在准确的时间设置闹钟?

AlarmActivity.java

package com.javapapers.androidalarmclock;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import java.util.Calendar;

public class AlarmActivity extends Activity {

AlarmManager alarmManager;
private PendingIntent pendingIntent;
private TimePicker alarmTimePicker;
private static AlarmActivity inst;
private TextView alarmTextView;

public static AlarmActivity instance() {
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    alarmTimePicker = (TimePicker) findViewById(R.id.alarmTimePicker);
    alarmTextView = (TextView) findViewById(R.id.alarmText);
    ToggleButton alarmToggle = (ToggleButton)     findViewById(R.id.alarmToggle);
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}

public void onToggleClicked(View view) {
    if (((ToggleButton) view).isChecked()) {
        Log.d("MyActivity", "Alarm On");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,   alarmTimePicker.getCurrentHour());
        calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
        Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0, myIntent, 0);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.cancel(pendingIntent);
        setAlarmText("");
        Log.d("MyActivity", "Alarm Off");
    }
}

public void setAlarmText(String alarmText) {
    alarmTextView.setText(alarmText);
}
}

AlarmReceiver.java

package com.javapapers.androidalarmclock;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;

public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    //this will update the UI with message
    AlarmActivity inst = AlarmActivity.instance();
    inst.setAlarmText("Alarm! Wake up! Wake up!");

    //this will sound the alarm tone
    //this will sound the alarm once, if you wish to
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();

    //this will send a notification message
    ComponentName comp = new ComponentName(context.getPackageName(),
            AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}

AlarmService.java

package com.javapapers.androidalarmclock;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class AlarmService extends IntentService {
private NotificationManager alarmNotificationManager;

public AlarmService() {
    super("AlarmService");
}

@Override
public void onHandleIntent(Intent intent) {
    sendNotification("Wake Up! Wake Up!");
}

private void sendNotification(String msg) {
    Log.d("AlarmService", "Preparing to send notification...: " + msg);
    alarmNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, AlarmActivity.class), 0);

    NotificationCompat.Builder alamNotificationBuilder = new   NotificationCompat.Builder(
            this).setContentTitle("Alarm").setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);


    alamNotificationBuilder.setContentIntent(contentIntent);
    alarmNotificationManager.notify(1, alamNotificationBuilder.build());
    Log.d("AlarmService", "Notification sent.");
}
}

请帮忙!我很绝望。

如果你想有24小时的跨度,你还需要设置天,这就是为什么你不能把时间设置在phone当前时间之前。

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
if(calendar.getTimeInMillis() < System.currentTimeMillis())
       calendar.add(Calendar.DAY, 1);

我只想分享我发现的关于闹钟延迟问题的答案