android - Keeping count of notifications in a broadcast receiver -


i trying keep count going on number of unread notifications in broadcast receiver , have number of unread notifications display differently accumulate every time receiver fired going re-initialize , clear out count. how can keep control of count, going have create class keep variables? seems lot of work simple

if you're looking persist value between instantiations of broadcastreceiver, store result in private preferences object. can read stored value in @ beginning of each onreceive(), , write out @ end. like:

public static final string prefs_name = "com.examples.myapplication.prefs"; public static final string key_count = "notificationcount";  private int currentcount;  public void onreceive(context context, intent intent) {     sharedpreferences values = context.getsharedpreferences(prefs_name, activity.mode_private);     currentcount = values.getint(key_count, 0);  //sets 0 if not in prefs yet      //do magic work here      //write value storage later use     sharedpreferences.editor editor = values.edit();     editor.put(key_count,currentcount);     editor.commit(); } 

you write global standard preferences preferencemanager.getdefaultsharedpreferences(context) instead, wouldn't require define name.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -