开发者

(Newbie) Can someone please give me an example of activity-level preferences?

Ok, this is how I have done the example of "Shared preferences", this is in my helper preferences class开发者_C百科:

public static final String GAME_PREFERENCES = "GamePrefs";

and this is in one of my activitie's classes:

     SharedPreferences settings = 
         getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE); 
         SharedPreferences.Editor prefEditor = settings.edit(); 
         prefEditor.putString("lastLaunch", returnTimeAndDateFormatted()); 
         prefEditor.commit();


         SharedPreferences settings2 = 
             getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
             if (settings2.contains("lastLaunch") == true) { 
//               System.err.println(settings2.getString("lastLaunch", "Default"));
                 Log.i("LASTLAUNCH", settings2.getString("lastLaunch", "Not LastLaunch value found!"));
             }

As I understand it I can access the variable "lastLaunch" from any of my activity classes which is all fine and dandy.

My book says there is also something known as "activity-level preferences" but does not give an example :(

Can someone give me an example (code) of this please?

Thanks in advance!


An example would be to use the getPreferences() method of activity.

SharedPreferences settings = getPreferences(MODE_PRIVATE); 
SharedPreferences.Editor prefEditor = settings.edit(); 
prefEditor.putString("lastLaunch", returnTimeAndDateFormatted()); 
prefEditor.commit();

SharedPreferences settings2 = getPreferences(MODE_PRIVATE);
if (settings2.contains("lastLaunch") == true) { 
    Log.i("LASTLAUNCH", settings2.getString("lastLaunch", "Not LastLaunch value found!"));
}

Here lastLaunch is private to this Activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜