如何在android中设置具有不同时序的多个警报

时间:2014-05-29 05:17:33

标签: android android-alarms

我是android的新手。我正在使用闹钟管理器做一个Android应用程序。我需要在一天中设置三次重复警报,例如上午10点,下午晚上9点。所以警报应该被设置为时间每天。下面是我试过的代码。但它会在晚上9点(以前)响起。我怎么解决这个问题?谢谢。

代码: 公共类AlarmManagerBroadcastReceiver扩展BroadcastReceiver {

final public static String ONE_TIME = "onetime";
public static MediaPlayer mp = null;
Context ctx;

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context
            .getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    // Acquire the lock
    wl.acquire();
    ctx = context;

    mp = MediaPlayer.create(context, R.raw.audio);
    playSound(context, getAlarmUri());

            // The DialogClass will make alert dialog for indication
    Intent i = new Intent(context, DialogClass.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);

    // Release the lock
    wl.release();

}

private void playSound(final Context context, Uri alert) {

    Thread background = new Thread(new Runnable() {
        public void run() {
            try {

                mp.start();

            } catch (Throwable t) {
                Log.i("Animation", "Thread  exception " + t);
            }
        }
    });
    background.start();
}

private Uri getAlarmUri() {

    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alert == null) {
        alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (alert == null) {
            alert = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        }
    }
    return alert;
}

public void SetAlarm(Context context) {

    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    AlarmManager am1 = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    AlarmManager am2 = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 10);
    c.set(Calendar.MINUTE, 0);

    Calendar c1 = Calendar.getInstance();
    c1.set(Calendar.HOUR_OF_DAY, 14);
    c1.set(Calendar.MINUTE, 0);

    Calendar c2 = Calendar.getInstance();
    c2.set(Calendar.HOUR_OF_DAY, 21);
    c2.set(Calendar.MINUTE, 0);

    Intent intent = new Intent(context,
            AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
    am1.setRepeating(AlarmManager.RTC_WAKEUP, c1.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
    am2.setRepeating(AlarmManager.RTC_WAKEUP, c2.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);

}

...

1 个答案:

答案 0 :(得分:0)

查看以下内容的答案。Android Set Multiple Alarms

希望这对你有很大的帮助。