儲存密碼的Activity:
public class SharedPreferenceTest extends Activity { /** Called when the activity is first created. */private ImageView mSkip; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSkip = (ImageView)findViewById(R.id.mSkip); mSkip.setOnClickListener(new OnClickListener(){public void onClick(View v) {// TODO Auto-generated method stubIntent localIntent = new Intent();localIntent.setClass(getApplicationContext(), CopyOfSharedPreferenceTest.class);SharedPreferenceTest.this.startActivity(localIntent);} }); SharedPreferences.Editor spf =getSharedPreferences("USERPASS",PreferenceActivity.MODE_WORLD_READABLE).edit(); spf.putString("passWord", "hello"); spf.commit(); }}
讀取密碼的Activity:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SharedPreferences sp = getSharedPreferences("USERPASS",PreferenceActivity.MODE_PRIVATE); Log.i("xiao", "passwd = " + sp.getString("passWord", "123")); }