移动应用开发 实验报告8 Android数据存储技术-SharedPreferences
一、实验目的 熟悉SharedPreferences对象; 基于SharedPreferences对象进行读写。 |
二、实验内容 编写一个程序,向文件中保存两种数据:string型和int型 |
三、实验原理 SharedPreferences是一种轻量级的数据存储方式,采用键值对的存储方式。
SharedPreferences只能存储少量数据,大量数据不能使用该方式存储,支持存储的数据类型有booleans, floats, ints, longs, and strings。
SharedPreferences存储到一个XML文件中的,路径在/data/data/<packagename>/shared_prefs/下。 |
四、实验代码及结果 package com.example.fff;
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.content.*; import android.widget.*; public class MainActivity extends Activity { private static final String f="Luz"; private TextView authorinfo=null; private TextView ageinfo=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.authorinfo=(TextView)super.findViewById(R.id.authorinfo); this.ageinfo=(TextView)super.findViewById(R.id.ageinfo); SharedPreferences share=super.getSharedPreferences(f, Activity.MODE_PRIVATE); SharedPreferences.Editor edit=share.edit(); edit.putString("author","Luz"); edit.putInt("age0",30); edit.commit(); this.authorinfo.setText("作者:"+share.getString("author", "没有作者信息.")); this.ageinfo.setText("年龄:"+share.getInt("age0",0)); }
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
}
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TextView android:id="@+id/authorinfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="22sp" />
<TextView android:id="@+id/ageinfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/authorinfo" android:textColor="#000000" android:textSize="22sp" android:layout_marginTop="50dip"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fff" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.fff.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
|
五、试验分析(思考) ShredPreferences是单例对象,第一次打开后,之后获取都无需创建,速度很快。 当第一次获取数据后,数据会被加载到一个缓存的Map中,之后的读取都会非常快。 当由于是XML<->Map的存储方式,所以,数据越大,操作越慢,get、commit、apply、remove、clear都会受影响,所以尽量把数据按功能拆分成若干份。
|