Допустим у меня есть array времен. Назовем ее timeList
. Она содержит в себе следующее: {"11:40","11:55","16:15","20:05"}
. Сперва, я должен показать notification в 11:40
, потом в 11:55
и так далее.Каждый notification имеет свой title
и text
. И они не должны обновляться(то есть если notification показался в 11:40
и user не убрал или не нажал на него, то следующий notification в 11:55
, должен расположиться рядом с ним). Я знаю как показывать notification используя AlarmManager
и BroadcastReceiver
. Но не могу представить,как решить эту задачу. Но есть такое предположение: Когда приложение открывается в первый раз , я устанавливаю alarm, чтобы показать уведомление по index= 0
( где index
-индекс массива timeList
). Когда первое уведомление было показано , мне следует обновить index
и установить alarm на следующее уведомление и так далее , до конца. В конце ,отменяется все операции и уведомление больше показываться не будет. Но я не представляю, как написать эту идею. Как решить эту проблему? Правильный ли мой подход? Мне нужен Service
для этого? Можете ли вы дать конкретные шаги для решения этой проблемы? Ниже мой код:
public void setAlarm(){
Calendar calendar = Calendar.getInstance() ;
//храню id в Preferense
calendar.set(Calendar.HOUR_OF_DAY,timeList.get(id).hour_of_day) ;
calendar.set(Calendar.MINUTE,timeList.get(id).minute) ;
calendar.set(Calendar.SECOND,timeList.get(id).second) ;
Intent intent = new Intent(this,MyHopingReceiver.class) ;
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),3*60*1000,pendingIntent);
}
Метод onReceiver
BroadCastReceiver-а
:
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeatingIntent = new Intent(context,SomeActivity.class);
repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeatingIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(android.R.drawable.arrow_down_float)
.setContentTitle("title")
.setContentText("text")
.setAutoCancel(true);
notificationManager.notify(100,builder.build());
//Как здесь обновить index и удалить предыдущий alarm,а потом
//установить новый?
}