SharedPreferences類,它是一個輕量級的存儲類,特別適合用於保存軟件配置參數。
SharedPreferences保存數據,其背后是用xml文件存放數據,文件存放在/data/data/<package name>/shared_prefs目錄下:
一、首先分析幾個方法:
1、getSharedPreferences(name,mode)
方法的第一個參數用於指定該文件的名稱,名稱不用帶后綴,后綴會由Android自動加上;
方法的第二個參數指定文件的操作模式,共有六種操作模式。
六種操作模式分別為:
1). MODE_APPEND: 追加方式存儲
2). MODE_PRIVATE: 私有方式存儲,其他應用無法訪問
4). MODE_MULTI_PROCESS:多個進程間共享,但在2.3之后不再使用了
5). MODE_WORLD_READABLE: 表示當前文件可以被其他應用讀取(不推薦使用)
6). MODE_WORLD_WRITEABLE: 表示當前文件可以被其他應用寫入(不推薦使用)
SharedPreferences share = getSharedPreferences("lsf", MODE_PRIVATE);
2、edit()方法獲取editor對象
editor存儲對象采用key-value鍵值對進行存放
通過commit()方法提交數據
SharedPreferences share = getSharedPreferences("jackie", MODE_WORLD_READABLE); //其他應用可以獲得
SharedPreferences.Editor edit = share.edit();
edit.putString(“password”, ""+987654321);
edit.putBoolean("pass_state", true);
edit.commit();
與之對應其他應用獲取數據的方法:
otherAppsContext = createPackageContext("com.jackie.contextprovider", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("jackie", Context.MODE_WORLD_READABLE);
String password= sharedPreferences.getString("password", "");
boolean pass_state= sharedPreferences.getBoolean("pass_state", false); //第二個參數為缺省值,如果preference中不存在該key,將返回缺省值
mTextView.setText("password:"+password+"||||||||"+"pass_state:"+pass_state);
如果你想要刪除通過SharedPreferences產生的文件,可以通過以下方法:
File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","Activity.xml"); if(file.exists()){ file.delete(); Toast.makeText(TestActivity.this, "刪除成功", Toast.LENGTH_LONG).show(); }
二、應用實例:
一個應用中設置一個按鈕,可按下觸發設置為共享或不共享其SharedPreferences產生的文件,
另一個應用通過按鈕按下觸發獲得之前應用中的數據。
1、共享數據文件的應用:
package com.jackie.contextprovider; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { public static final String NEW_LIFEFROM_DETECTED = "com.jackie.contextprovider"; public static final String TAG = "testprovider"; private Button snedButton; private boolean isShare = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); snedButton = (Button) findViewById(R.id.send_message); } public void sendMessage(View v){ if(isShare){ SharedPreferences share = getSharedPreferences("jackie", MODE_WORLD_READABLE); SharedPreferences.Editor edit = share.edit(); edit.putString(“password”, ""+987654321); edit.putBoolean("pass_state", true); edit.commit(); isShare = false; snedButton.setText(R.string.no_send); }else{ SharedPreferences share = getSharedPreferences("jackie", MODE_PRIVATE); SharedPreferences.Editor edit = share.edit(); edit.clear(); edit.commit(); isShare = true; snedButton.setText(R.string.send); } } }
布局配置文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/send_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" android:text="@string/send" /> </RelativeLayout>
2、讀取共享數據的應用
package com.jackie.contextuser; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button mButton; TextView mTextView; Context otherAppsContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.get_message); mTextView = (TextView) findViewById(R.id.write_message); otherAppsContext = null; } @Override protected void onResume(){ super.onResume(); otherAppsContext = null; } @Override protected void onStop(){ super.onStop(); otherAppsContext = null; } public void getMessage(View v) throws NameNotFoundException{ otherAppsContext = createPackageContext("com.tcl.contextprovider", Context.CONTEXT_IGNORE_SECURITY); SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("jackie", Context.MODE_WORLD_READABLE); String password = sharedPreferences.getString("password", ""); boolean pass_state= sharedPreferences.getBoolean("pass_state", false); mTextView.setText("password :"+password +"||||||||"+"pass_state:"+pass_state); } }
布局配置文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/get_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="getMessage" android:text="@string/get"/> <TextView android:id="@+id/write_message" android:layout_below="@id/get_message" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
