安卓学习-数据存储与IO-SharedPreferences

安卓学习-数据存储与IO-SharedPreferences

 

运行完后,可以在DDMS,看到这个xml文件

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

    EditText editText1;
    TextView textView3;
    SharedPreferences pre;
    Editor editor;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editText1=(EditText)findViewById(R.id.editText1);
        textView3=(TextView)findViewById(R.id.textView3);
        
        pre=getSharedPreferences("myPre222", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
        editor=pre.edit();
        
        Button btn1=(Button)findViewById(R.id.button1);
        Button btn2=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    public void onClick(View v) {
        if(v==findViewById(R.id.button1)){
            editor.putString("name", editText1.getText()+"");
            editor.commit();
        }else if(v==findViewById(R.id.button2)){
            String name=pre.getString("name", "");
            textView3.setText(name);
        }
    }
}
View Code

activity_main.xml

<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}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="姓名"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="22dp"
        android:text="写入" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="36dp"
        android:text="读取" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginLeft="11dp"
        android:layout_toRightOf="@+id/button2"
        android:text="值:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
View Code

 别的应用程序来读取这个xml

try {
                    Context context=createPackageContext("com.example.fffff", Context.CONTEXT_IGNORE_SECURITY);
                    SharedPreferences pre=context.getSharedPreferences("myPre222", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
                    String name=pre.getString("name", "");
                    Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                } catch (NameNotFoundException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
View Code

 MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE已经在Android 4.2(API level 17)废弃了,因为这样危险,安全性不高。

posted on 2015-01-04 13:56  weijj  阅读(143)  评论(0编辑  收藏  举报

导航