Java tutorial
//package com.java2s; //License from project: Open Source License import android.annotation.TargetApi; import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import android.preference.PreferenceManager; public class Main { private static final String KEY_ACCOUNT_NAME = "account_name"; private static String mCurrentUser = null; @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static void setAccountName(Context ctx, String accountName) { SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); editor.putString(KEY_ACCOUNT_NAME, accountName); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { editor.apply(); } else { editor.commit(); } mCurrentUser = accountName; } }