我有一个服务,可以接收通知(使用Google Cloud Messaging),并通知用户。在该服务中,我还使用SharedPreferences存储由Cloud Messaging发送的消息。我将这些消息收集到一个HashSet中,并且既不删除该HashSet也不删除其中的任何元素。这很重要,因为我的某个活动必须显示整个消息列表。
这个工作正常,除非用户使用“最近使用的应用程序”按钮杀死应用程序。当他这样做并重新启动应用程序时,某些消息无法被活动检索,因此我猜想其中一些消息已被某种方式删除。
我是否没有正确使用SharedPreferences?我应该怎么做才能避免这种情况?以下是我的服务代码:(onHandleIntent方法的相关部分)
这个工作正常,除非用户使用“最近使用的应用程序”按钮杀死应用程序。当他这样做并重新启动应用程序时,某些消息无法被活动检索,因此我猜想其中一些消息已被某种方式删除。
我是否没有正确使用SharedPreferences?我应该怎么做才能避免这种情况?以下是我的服务代码:(onHandleIntent方法的相关部分)
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Notifications.class), 0);
String message=extras.getString("message");
sharedpreferences=this.getSharedPreferences(Constantes.PREFERENCES,Context.MODE_PRIVATE);
Set<String> setMessages= sharedpreferences.getStringSet("SETMESSAGES", new HashSet<String>());
setMessages.add(message);
Editor editor = sharedpreferences.edit();
editor.putStringSet("SETMESSAGES", setMessages);
editor.commit();
//The notification:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("New Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText("You got some new message!"))
.setContentText("You got some new message!")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());