Android SharedPreferences(信息的保存和读取)
Android SharedPreferences(信息的保存和读取)
//main.xml,主布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/name" />
- <EditText
- android:id="@+id/webname"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/age"
- >
- </TextView>
- <EditText
- android:id="@+id/age"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <Button
- android:id="@+id/saveButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/save"
- />
- <Button
- android:id="@+id/restorationData"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/huifu"
- />
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello SharedPreferencesActivity!</string>
- <string name="app_name">软件参数保存</string>
- <string name="name">网名</string>
- <string name="age">年龄</string>
- <string name="save">保存参数</string>
- <string name="success">保存成功</string>
- <string name="huifu">恢复参数</string>
- </resources>
- package sn.len.sharedpreferences;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class SharedPreferencesActivity extends Activity
- {
- private EditText name=null;
- private EditText age=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- name=(EditText)findViewById(R.id.webname);
- age=(EditText)findViewById(R.id.age);
- Button saveButton=(Button)findViewById(R.id.saveButton);
- Button restorationButton=(Button)findViewById(R.id.restorationData);
- //写入信息
- saveButton.setOnClickListener
- (
- new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- String web_name=name.getText().toString();
- String true_age=age.getText().toString();
- SharedPreferences preferences=getSharedPreferences("softinfo",Context.MODE_WORLD_READABLE);
- Editor edit=preferences.edit();
- edit.putString("name", web_name);
- edit.putInt("age",new Integer(true_age));
- edit.commit();
- Toast.makeText(SharedPreferencesActivity.this, R.string.success,Toast.LENGTH_LONG).show();
- }
- }
- );
- //读取信息
- restorationButton.setOnClickListener
- (
- new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- SharedPreferences ferences=getSharedPreferences("softinfo",0);
- String true_name=ferences.getString("name", "");
- int true_age=ferences.getInt("age", 20);
- name.setText(true_name);
- age.setText(String.valueOf(true_age));
- }
- }
- );
- }
- }
//运行结果
点击保存按钮,成功后消除内容,再点恢复数据,也就是重新读取XML的值放在EditText中
//重其它App中,访问这个APP的信息,如下代码
- package sn.len.others;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.test.AndroidTestCase;
- import android.util.Log;
- public class SharPreferencesSoftinfoTest extends AndroidTestCase
- {
- private static final String TAG="SharePreferencesContent";
- public void testAccessSharedPreferences() throws NameNotFoundException
- {
- //通过本应该程序的上下文来建想要访问App的上下文对象
- //sn.len.sharedpreferences 存放softinfo.xml那个包名称
- //CONTEXT_IGNORE_SECURITY 取消跨App的的访问安全
- Context accessSharePreferences=getContext().createPackageContext("sn.len.sharedpreferences",Context.CONTEXT_IGNORE_SECURITY);
- SharedPreferences shared=accessSharePreferences.getSharedPreferences("softinfo", Context.MODE_PRIVATE);
- String name=shared.getString("name", "");
- int age=shared.getInt("age", 18);
- Log.i(TAG, "name:"+name+"age:"+age);
- }
- }
评论暂时关闭