如何在特定日期的不同时间显示多个通知

时间:2015-11-20 07:39:01

标签: android notifications

在我的Android应用程序中,我需要在特定日期显示8个通知,例如(21-11-2015)在不同的时间,我能够如何使用警报管理器类只有1个通知,但我也应该显示剩下7个不同内容的通知。

这是MyActivity

static HashSet<Tuple<int, int>> PairsThatSumToN(int[] arr, int N)
{
    HashSet<int> hash = new HashSet<int>(arr);
    HashSet<Tuple<int, int>> result = new HashSet<Tuple<int, int>>(new IntTupleComparer());

    foreach(int i in arr)
    {
        int j = N - i;
        if (hash.Contains(j)) result.Add(new Tuple<int, int>(i, j));
    }
    return result;
}

public class IntTupleComparer : IEqualityComparer<Tuple<int, int>>
{
    public bool Equals(Tuple<int, int> x, Tuple<int, int> y)
    {
        return (x.Item1 == y.Item1 && x.Item2 == y.Item2) || (x.Item1 == y.Item2 && x.Item2 == y.Item1);
    }

    public int GetHashCode(Tuple<int, int> obj)
    {
        return (obj.Item1 + obj.Item2).GetHashCode();
    }
}

这是AlarmReceiver

Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.clear();
    cal.set(2015, 10, 20, 14, 18);
    cal.set(2015, 10, 20, 14, 20);

    Intent myIntent1 = new Intent(this, AlarmBroadCustReciver.class);

    Intent myIntent2 = new Intent(this, AlarmBroadCustReciver.class);

    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1253, myIntent1,
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1263, myIntent2,
            PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent1);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);

1 个答案:

答案 0 :(得分:1)

在日历对象中设置不同的时间,然后使用该对象使用AlarmManager设置警报。

您可以同时设置所有闹钟,它们将在设置时执行。

您的代码(由我修改): -

    AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    Intent myIntent1 = new Intent(this, AlarmBroadCustReciver.class);
    myIntent1.setAction(ACTION_ONE);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1253, myIntent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Set the time for first alarm here
    cal.set(2015, 10, 20, 14, 18);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent1);
    Intent myIntent2 = new Intent(this, AlarmBroadCustReciver.class);
    myIntent2.setAction(ACTION_TWO);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 1263, myIntent2,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Set the time for second alarm here
    cal.set(2015, 10, 20, 14, 20);
    alarmManager1.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent2);
    // In this way set time for all the rest of the alarms

您的BroadcastReceiver类将如下所示: -

    public class AlarmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
        builder.setContentTitle(context.getString(R.string.app_name));
        if (intent.getAction().equalsIgnoreCase(ACTION_ONE)) {
            builder.setContentText("Alarm one");
        } else {
            builder.setContentText("Alarm two");
        }
        Notification notification = builder.build();
        int notificationID = 0;
        notificationManager.notify(notificationID, notification);
        // If you want two notifications simultaneously, change the notificationID (written in above line of code) for every notification.
    }
}

请注意,ACTION_ONE和ACTION_TWO是两个字符串常量