为两个不同的android设置相同的警报

时间:2015-07-16 09:15:02

标签: android android-alarms

我想在一分钟,两分钟和一两分钟之前设置闹钟。实际上用户将从微调器中选择一个值。警报在一分钟和两分钟之前成功启动。但是对于前一分钟和两分钟,它只执行一次而不是两次。我写了这段代码

    private void createPendingIntent(String name, int id, String time, String date){

        Intent intent = new Intent(getApplicationContext(), VisitReminderNotificationMessage.class);
        intent.putExtra("Id", id);
        intent.putExtra("Doctor Name", name);
        intent.putExtra("Date", date);
        intent.putExtra("Time", time);

        pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, 0);
    }

private void createReminders(long timeInMilliseconds, String remindsBefore){

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    long newTimeInMilliseconds;

    if (remindsBefore.equals("1 min")) {
        newTimeInMilliseconds = timeInMilliseconds - 1 * 60 *1000;

        alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds, pendingIntent);
    } else if (remindsBefore.equals("2 min")) {
        //Convert 24 Hrs to milliseconds
        newTimeInMilliseconds = timeInMilliseconds - 2* 60 * 1000;

        alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds, pendingIntent);
    } else if (remindsBefore.equals("Both 1 and 2 min")){

        //AlarmManager alarmManager1 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        long newTimeInMilliseconds1 = timeInMilliseconds - 1 * 60 * 1000;
        long newTimeInMilliseconds2 = timeInMilliseconds - 2* 60 * 1000;

        alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds1, pendingIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds2, pendingIntent1);
    }
}

And broadcast receiver that I have used to show notification is

    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.support.v4.app.NotificationCompat;

    import com.spirant.epilife.R;
    import com.spirant.epilife.activity.VisitReminderActivity;

    public class VisitReminderNotificationMessage extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            Intent intent1 = new Intent(context, VisitReminderActivity.class);

            String doctorName = intent.getStringExtra("Doctor Name");
            int notificationId = intent.getIntExtra("Id", 0);
            String date = intent.getStringExtra("Date");
            String time = intent.getStringExtra("Time");

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
            // Build the notification
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                    .setContentText("Doctor Name: " + doctorName + "\n" + "Date: " + date
                            + "\n" + "Time: " + time)
                    .setContentTitle("Visit your Doctor")
                    .setSmallIcon(R.drawable.app_icon)
                    .setCategory(NotificationCompat.CATEGORY_ALARM)
                    .setContentIntent(pendingIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("Doctor Name: " + doctorName + "\n" + "Date: " + date
                            + "\n" + "Time: " + time))
                    .setSound(alarmSound)
                    .setAutoCancel(true);

            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            // Pass the notification to the notification manager
            mNotificationManager.notify(notificationId, notificationBuilder.build());
        }
    }

请有人帮忙..

1 个答案:

答案 0 :(得分:0)

你必须在发送通知后再次调用在VisitReminderNotificationMessage的onReceiver中设置alarmManager

 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
     alarmManager.set(AlarmManager.RTC_WAKEUP, newTimeInMilliseconds1, pendingIntent);