现在的位置: 首页 > 综合 > 正文

SharedPrepferences存取其他程序的总结

2018年02月05日 ⁄ 综合 ⁄ 共 2933字 ⁄ 字号 评论关闭


public class SharedPreferencesTestActivity extends Activity {
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		preferences = getSharedPreferences("count", MODE_WORLD_WRITEABLE);
		editor = preferences.edit();
		Button read = (Button) findViewById(R.id.read);
		Button write = (Button) findViewById(R.id.write);
		//设置两个按钮的单击事件 分别为写入数据和读取数据
		read.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				String time = preferences.getString("time", null);
				//根据key得到数据
				int count=preferences.getInt("count",0);
				int randnum = preferences.getInt("random", 0);
				String result = time == null ? "您暂时还没输入数据" : "写入的时间为:" + time
						+ "\n上次生成的随机数为: " + randnum+"/n登陆次数为:"+count;
				Toast.makeText(SharedPreferencesTestActivity.this, result, 5000)
						.show();
			}
		});
		write.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {


				SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"
						+ "hh:mm:ss");
				int count=preferences.getInt("count",0);
				editor.putString("time", sdf.format(new Date()));
				editor.putInt("random", (int) (Math.random() * 10));
				editor.putInt("count",++count);
				editor.commit();
			}


		});


	}
}

在这个程序中写入三个key 分别为count 和random和time 通过Toast.makeText().show();在这个程序中显示

另一个程序获取这个程序存储的数据,并修改这个文件,增加key为name的 把名字设置成bill1,具体代码:

@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	
		Context useCount=null;
		try {
			//test.sharePreferences为上一个程序的包名,此处万不能指定错
			useCount=createPackageContext("test.sharePreferences",Context.CONTEXT_IGNORE_SECURITY);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//获取Sharedpreferences对象,此处count为文件名,即count.xml
		SharedPreferences prefs=useCount.getSharedPreferences("count",Context.MODE_WORLD_WRITEABLE);
		int count=prefs.getInt("count",100);
		String time=prefs.getString("time", null);
		System.out.println("count-------------"+count);
		System.out.println("time-------------"+time);
		//3个TextView 用来显示不同的信息
		TextView show1=(TextView)findViewById(R.id.show1);
		TextView show2=(TextView)findViewById(R.id.show2);
		TextView show3=(TextView)findViewById(R.id.show3);
		show1.setText("应用程序 以前被使用了"+count+"次");
		show2.setText("time:"+time);
		//Editor 用来编辑数据 增加name 
		Editor editor=prefs.edit();
		editor.putString("name","bill1");
		editor.commit();
		String name=prefs.getString("name", null);
		show3.setText("名字:"+name);
	}

当然,我们也可以在第一个程序里放入name,在后一个里修改这个key

总结:

1,Android根据应用程序的报名来作为该程序的标识的,所以我们获取另一个程序的数据必须根据

Context useCount=createPackageContext("test.sharePreferences",Context.CONTEXT_IGNORE_SECURITY);

来获取另一个程序的Context,再根据Context的getSharedPreferences(文件名,权限)获取对象

2,SharedPreferences对象根据getString,getInt来获取文件下的指定key 

3,如果想往文件下写字段,则必须通过

Editor editor=prefs.edit();

获取Editor对象,通过editor.putString()或者putInt()来放入字段

4,本来这是个很简单的实验,但是有个细节,让我调了2 3个小时,几近崩溃的地步,

在main。xml中 由于

   

<TextView
            android:id="@+id/show1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             /> 

指定的宽和高都设置成fill_parent导致只能显示一个textView 

细节性东西还需要注意。

在学习Android的路上继续努力!!!


抱歉!评论已关闭.