//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>
//strings.xml常量字符串
- <?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>
//SharedPreferencesActivity.java 處理類
- 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);
- }
- }