保存数据(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();	
		}



阅读更多
文章标签: string android 旅游
个人分类: android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭