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
Post a Comment