android 移动开发 SharedPreferences存储

  sharedpreferences 用于保存少量数据,格式简单的entry对,各种配置信息等。
   sharedpreferences接口主要负责读取应用中preferences数据,调用edit()方法 返回editor对象,而后进行相应的写入数据。
   context给我们提供了getSharedPreferences(String name,int mode)方法返回sharedpreferences实例。
数据默认保存在/data/data/。。。。packagename。。。。/shared_prefs目录下,以xml形式存在。


找到目录导出 查看如下格式、结果;


<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">era </string>
<int name="age" value="522" />
</map>




 一、在当前应用中


java代码实现-----------------------------------------------------------------------------------------------
================================================================



package com.baidu.preferences;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class PreferencesInActivity extends Activity {
private Button read, write;
private EditText age, name;
private SharedPreferences sp;
private SharedPreferences.Editor spEditor;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences_in);
read = (Button) findViewById(R.id.read);
write = (Button) findViewById(R.id.write);
age = (EditText) findViewById(R.id.age);
name = (EditText) findViewById(R.id.name);
sp = getSharedPreferences("value", Context.MODE_WORLD_READABLE);
spEditor = sp.edit();
read.setOnClickListener(listen);
write.setOnClickListener(listener);
}


// 读取数据
OnClickListener listen = new OnClickListener() {


@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
sp.getString("name", null) + sp.getInt("age", 0), 3000)
.show();
}
};
// 写入数据
OnClickListener listener = new OnClickListener() {


@Override
public void onClick(View v) {
int s = Integer.parseInt(age.getText().toString());
spEditor.putString("name", name.getText().toString());
spEditor.putInt("age", s);
spEditor.commit();
}
};
}



 对应的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=".PreferencesInActivity" >


    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/age"
        android:layout_centerVertical="true"
        android:layout_marginLeft="17dp"
        android:text="写入" />


    <Button
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/write"
        android:layout_alignRight="@+id/age"
        android:layout_marginRight="20dp"
        android:text="读取" />


    <EditText
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/write"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="42dp"
        android:inputType="number"
        android:singleLine="true"
        android:ems="10"
        android:hint="请输入年龄" >


    </EditText>


    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/age"
        android:layout_alignLeft="@+id/age"
           android:singleLine="true"
        android:layout_marginBottom="18dp"
        android:ems="10"
        android:hint="请输入名字" />


</RelativeLayout>








二、跨应用Sharedpreferences
     1、 创建数据共享的具体环境(报名作为一个程序的标识)
                  Context context= context.CreatePackageContext(包名    ,  环境权限);
     2、用具体标识环境调用相应的方法,如产生实例等,操作共享数据。
-------------------------------------------------------------------------------------------------------------------
==================================================================

package com.baidu.preferencesout;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class PreferencesSharedActivity extends Activity {
private Button readOut;
private SharedPreferences sp;
private Context con;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences_shared);
readOut = (Button) findViewById(R.id.readOut);


try {
con = createPackageContext("com.baidu.preferences",
Context.CONTEXT_IGNORE_SECURITY);

} catch (NameNotFoundException e) {
e.printStackTrace();
}

readOut.setOnClickListener(l);


}


OnClickListener l = new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sp = con.getSharedPreferences("value", Context.MODE_WORLD_READABLE);
Toast.makeText(
getApplicationContext(),
sp.getString("name", null)
+ sp.getInt("age", 0), 3000).show();

}
};
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值