为了保存软件的参数,或者是某些比较小型的数据,Android中我们可以使用Android为我么提供的SharedPreference类他是一个轻量级的储存类,特别适合用于保存软件参数使用SharedPreference保存数据,其背后是使用xml文件存放数据
文件存放在/data/data/<-package name->/shared_prefs目录下
实例
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/buttonWrite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写入数据" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/buttonWrite" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:id="@+id/buttonReade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取数据" android:layout_below="@+id/edit" android:layout_alignLeft="@+id/buttonWrite" android:layout_alignStart="@+id/buttonWrite" android:layout_marginTop="73dp" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="yu" android:textSize="20sp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="167dp" /> </RelativeLayout>
package xiaocool.net.sharedpreferencestest; import android.content.SharedPreferences; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends ActionBarActivity { //根据key读取数据 private SharedPreferences preferences; //写入数据 private SharedPreferences.Editor editor; private Button write,read; private EditText editText; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); write=(Button)this.findViewById(R.id.buttonWrite); read=(Button)this.findViewById(R.id.buttonReade); editText=(EditText)this.findViewById(R.id.edit); textView=(TextView)this.findViewById(R.id.textview); preferences=getSharedPreferences("xiaocool",MODE_PRIVATE); editor=preferences.edit(); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editor.putString("name",editText.getText().toString()); editor.commit(); editText.setText(""); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=preferences.getString("name",null); textView.setText(name); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
在其他的应用程序中访问
public class ReadOtherPreferences extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context context =null; try{ //获取其他应用程序对应的Context context=createPackageContext("xiaocool.net.sharedpreferencestest",Context.CONTEXT_IGNORE_SECURITY); }catch (PackageManager.NameNotFoundException e){ e.printStackTrace(); } //使用其他程序的Context 获取对应的.sharedpreferences SharedPreferences sharedPreferences=context.getSharedPreferences("YU",Context.MODE_WORLD_READABLE); //读取数据 String name=sharedPreferences.getString("name",null); Toast.makeText(ReadOtherPreferences.this,name,1).show(); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。