当前位置:操作系统 > 安卓/Android >>

Android数据存储

Android提供的数据持久化存储方式有以下几种。

1.Shared Preferences:以Key-Value形式存储数据


2.Internal Storage:数据文件存储在内存卡


3.External Storage:数据存储在外部设备,如SD卡等


4.SQLite Databases:SQLite存储方式


5.Network Connection:通过WebService等网络通信方式存储数据。

 

 

一、Shared Preferences

常用来存储应用中的用户偏好设置,例如应用的默认皮肤设置、记录用户上次登录信息等。数据的存储格式为Key-Value。

下面通过案例,记录用户信息来了解如何使用Shared Preferences。

介绍将使用到的Android SDK API:

SharedPreferences类:提供了一批读取、遍历数据的API。

Editor类:提供修改、保存数据API。
getSharedPreferences(String name, int mode):指定存储文件的文件名,并设置访问权限


getPreferences(int mode):如果你的Activity中只需要一个喜好文件,则可以不提供存储文件名,只需要设置访问权限


更多介绍可以访问:http://android.toolib.net/guide/topics/data/data-storage.html#pref

1、新建Android项目

项目名称Shared Preferences,主Activity名称为MainActivity。

2、设计主界面XML描述


[html] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/name" 
    /> 
  
<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/name" 
    /> 
 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/age"  
    /> 
     
<EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/age" 
    /> 
 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/save"  
    android:onClick="save" 
    /> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/name"
    />
 
<EditText
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/name"
 />

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/age"
 />
 
<EditText
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:id="@+id/age"
 />

<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/save"
 android:onClick="save"
 />
</LinearLayout>界面效果:
 
 \


 

3、定义“保存”按钮的save事件处理方法


[java] package mr.jin.shared; 
 
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.EditText; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
    private EditText nameEdit; 
    private EditText ageEdit; 
    private String USERINFO="userInfo"; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        nameEdit = (EditText) findViewById(R.id.name);//应用打开后,即查找到姓名、年龄的文本框对象。  
        ageEdit = (EditText) findViewById(R.id.age); 
 
        init(); 
    } 
     
    public void save(View v){ 
        try{ 
            SharedPreferences perference = getSharedPreferences(USERINFO, Context.MODE_PRIVATE); 
            Editor editor = perference.edit(); 
            editor.putString("name", nameEdit.getText().toString()); 
            editor.putString("age", ageEdit.getText().toString()); 
            editor.commit();//未调用commit前,数据实际是没有存储进文件中的。 调用后,存储存储  
             
            Toast.makeText(this, R.string.success, Toast.LENGTH_LONG).show(); 
             
        }catch(Exception e){ 
            Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show(); 
        } 
    } 
     
  &

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,