activity 之间共享sharedpreferences

public class AppPreferences {      private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; //  Name of the file -.xml      private SharedPreferences appSharedPrefs;      private Editor prefsEditor;      public AppPreferences(Context context)      {          this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);          this.prefsEditor = appSharedPrefs.edit();      }      public String getSmsBody() {          return appSharedPrefs.getString("sms_body", "");      }      public void saveSmsBody(String text) {          prefsEditor.putString("sms_body", text);          prefsEditor.commit();      } }
然后就可以

在你的 activity ...

protected AppPreferences appPrefs; appPrefs = new AppPreferences(getApplicationContext()); 
String someString = appPrefs.getSmsBody(); 

或者appPrefs.saveSmsBody(someString);

你可能感兴趣的:(xml)