In a previous question I asked about how to shuffle a list of activities, pick one, add it to intent and prevent repetition of already visited Activities, and I was fortunate to get the help of a very nice gentleman (@PedroFernandes) and solve my problem, here's the LINK to that question.
following on the same matter, I tried to implement that exact same method inside a Notification, what I want is to show a Notification daily in my app that takes the user to a different Activity every time. Although I managed to achieve this, my previous problem persists, the activities.remove(0);
is never triggered, and I keep getting duplicates, and the list never goes empty.
here's my Notification that extends a BroadcastReceiver:
Notification01.java
public class Notification01 extends BroadcastReceiver{
private static final int NOTIFICATION_ID = 1;
PendingIntent pendingIntent;
Notification notification;
TaskStackBuilder taskStackBuilder;
@Override
public void onReceive(Context context, Intent intent) {
ArrayList<Class> activities = new ArrayList<>();
activities.add(Activity01.class);
activities.add(Activity02.class);
activities.add(Activity03.class);
activities.add(Activity04.class);
activities.add(Activity05.class);
activities.add(Activity06.class);
activities.add(Activity07.class);
activities.add(Activity08.class);
activities.add(Activity09.class);
activities.add(Activity10.class);
if (activities.size()==0){
Toast.makeText(context, "ArrayList Empty", Toast.LENGTH_SHORT).show();
}
Collections.shuffle(activities);
Class activityToShow = activities.get(0);
intent = new Intent(context, activityToShow);
activities.remove(0);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
taskStackBuilder = TaskStackBuilder.create(context);
taskStackBuilder.addParentStack(Activity01.class);
taskStackBuilder.addNextIntent(intent);
pendingIntent = taskStackBuilder.getPendingIntent(1, PendingIntent.FLAG_CANCEL_CURRENT);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_logo_white)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentTitle("Notification Title")
.setContentText("Notification content")
.setStyle(new NotificationCompat.BigTextStyle().bigText("Notification Big Text"))
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
}
}
All help is appreciated, and I should also mention that I need this to persevere through reboots and app closes, I'm not sure if I should use SharedPreferences or Receivers ??!! I'm very new to this and have no Java experience whatsoever.
Many thanks in advance.
Aucun commentaire:
Enregistrer un commentaire