添加多个提醒 - 提醒应用
Adding multiple reminders - Reminder Application
我正在构建一个提醒应用程序,它应该能够设置多个提醒。我有一个 AddNewReminder 底部 sheet 视图,它有一个用于输入提醒名称的 editText、一个日期选择器对话框、一个时间选择器对话框和一个将数据保存到 SQLite 数据库并设置提醒的保存按钮。
AddNewReminder.java(保存按钮的 OnClickListener):-
String name = mReminderNameInsert.getText().toString();
id = new Random().nextInt(100);
Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
reminderReciever.putExtra("reminderName", name)
.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, reminderReciever, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, ih);
calendar.set(Calendar.MINUTE, imin);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_MONTH, iday);
calendar.set(Calendar.MONTH, imon);
long lTime = calendar.getTimeInMillis();
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, lTime, pendingIntent);
然后使用广播接收器来显示通知。
String mReminderName;
String CHANNEL_ID = "channel";
int id;
@Override
public void onReceive(Context context, Intent intent) {
mReminderName = intent.getStringExtra("reminderName");
id = intent.getIntExtra("id", 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "id");
mBuilder.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
.setContentTitle("It's Time..!!")
.setContentText(mReminderName + "" + id)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true);
NotificationManagerCompat compat = NotificationManagerCompat.from(context);
compat.notify(id, mBuilder.build());
}
显示提醒通知,但只显示最新的。 =] 显示为通知)..
我是否应该将警报管理器代码添加到 MainActivity 而不是 AddNewReminder 片段...
任何帮助将不胜感激
您在这两种情况下使用的 PendingIntent,一个用于 8:30 警报,另一个用于 8:31 警报,代表同一对象,因此调用 cancel()
删除旧对象。正如 official docs 提到的那样,
A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent#filterEquals(Intent), or different request code integers supplied to PendingIntents
所以试试下面的方法。
Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
reminderReciever.putExtra("reminderName", name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), id, reminderReciever, 0);
我正在构建一个提醒应用程序,它应该能够设置多个提醒。我有一个 AddNewReminder 底部 sheet 视图,它有一个用于输入提醒名称的 editText、一个日期选择器对话框、一个时间选择器对话框和一个将数据保存到 SQLite 数据库并设置提醒的保存按钮。
AddNewReminder.java(保存按钮的 OnClickListener):-
String name = mReminderNameInsert.getText().toString();
id = new Random().nextInt(100);
Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
reminderReciever.putExtra("reminderName", name)
.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, reminderReciever, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, ih);
calendar.set(Calendar.MINUTE, imin);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.DAY_OF_MONTH, iday);
calendar.set(Calendar.MONTH, imon);
long lTime = calendar.getTimeInMillis();
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.setExact(AlarmManager.RTC_WAKEUP, lTime, pendingIntent);
然后使用广播接收器来显示通知。
String mReminderName;
String CHANNEL_ID = "channel";
int id;
@Override
public void onReceive(Context context, Intent intent) {
mReminderName = intent.getStringExtra("reminderName");
id = intent.getIntExtra("id", 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "id");
mBuilder.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
.setContentTitle("It's Time..!!")
.setContentText(mReminderName + "" + id)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true);
NotificationManagerCompat compat = NotificationManagerCompat.from(context);
compat.notify(id, mBuilder.build());
}
显示提醒通知,但只显示最新的。 =] 显示为通知)..
我是否应该将警报管理器代码添加到 MainActivity 而不是 AddNewReminder 片段...
任何帮助将不胜感激
您在这两种情况下使用的 PendingIntent,一个用于 8:30 警报,另一个用于 8:31 警报,代表同一对象,因此调用 cancel()
删除旧对象。正如 official docs 提到的那样,
A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen
If you truly need multiple distinct PendingIntent objects active at the same time (such as to use as two notifications that are both shown at the same time), then you will need to ensure there is something that is different about them to associate them with different PendingIntents. This may be any of the Intent attributes considered by Intent#filterEquals(Intent), or different request code integers supplied to PendingIntents
所以试试下面的方法。
Intent reminderReciever = new Intent(getContext(), ReminderReceiver.class);
reminderReciever.putExtra("reminderName", name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), id, reminderReciever, 0);