保存数据(1)通过sessions保存key--value数据------SharedPreferences

存数据
 
 
                SharedPreferences mySharedPreferences = getSharedPreferences("test",
				Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		editor.putString("name", "李宁");
		editor.putString("habit", "Android、写作、旅游");
		editor.commit();

    取数据



SharedPreferences sharedPreferences = getSharedPreferences("test",
				Activity.MODE_PRIVATE);
		// 使用getXxx方法获得value,getXxx方法的第2个参数是value的默认值
		String name = sharedPreferences.getString("name", "");
		String habit = sharedPreferences.getString("habit", "");
		Toast.makeText(this, "name:" + name + "\n" + "habit:" + habit,
				Toast.LENGTH_LONG).show();


共享数据的权限示例


	super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		int[] modes = new int[]
		{ Activity.MODE_PRIVATE, Activity.MODE_WORLD_READABLE,
				Activity.MODE_WORLD_WRITEABLE, Activity.MODE_APPEND };
		for(int i = 0; i < modes.length; i++)
		{
			SharedPreferences mySharedPreferences = getSharedPreferences(
					"data" + String.valueOf(i + 1), modes[i]);
			SharedPreferences.Editor editor = mySharedPreferences.edit();
			editor.putString("name", "bill");
			editor.commit();	
		}



你可能感兴趣的:(android,String,旅游)