本講內(nèi)容:SharedPreferences 和 Android中的文件IO操作
1、SharedPreferences
2、Android中的文件IO操作
Android中進(jìn)行數(shù)據(jù)共享和數(shù)據(jù)存儲(chǔ)有多種方式,前面我們講過(guò)使用Sqlite數(shù)據(jù)庫(kù)的方式,今天我們講一下SharedPreferences和文件讀寫(xiě)操作方式。
一、SharedPreferences
SharedPreferences是一種輕量級(jí)的數(shù)據(jù)存儲(chǔ)方式,學(xué)過(guò)Web開(kāi)發(fā)的同學(xué),可以想象它是一個(gè)小小的Cookie。它可以用鍵值對(duì)的方式把簡(jiǎn)單數(shù)據(jù)類型(boolean、int、float、long和String)存儲(chǔ)在應(yīng)用程序的私有目錄下(data/data/包名/shared_prefs/)自己定義的xml文件中。下面我們用一個(gè)記錄音樂(lè)播放進(jìn)度的例子來(lái)學(xué)習(xí)SharedPreferences的使用。
1、建立一個(gè)新的項(xiàng)目 Lesson19_HelloSharedPreferences , Activity名字叫 MainHelloSharedPreferences.java
2、建立一個(gè)MusicService.java的Service,代碼如下:
- package android.basic.lesson19;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.widget.Toast;
- public class MusicService extends Service {
- //定義MediaPlayer播放器變量
- MediaPlayer mPlayer = new MediaPlayer();
- @Override
- public void onCreate() {
- Toast.makeText(getApplicationContext(), "Service onCreate()", Toast.LENGTH_LONG).show();
- //創(chuàng)建播放器
- mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.babayetu);
- //設(shè)置自動(dòng)循環(huán)
- mPlayer.setLooping(true);
- }
- @Override
- public IBinder onBind(Intent intent) {
- Toast.makeText(getApplicationContext(), "Service onBind()", Toast.LENGTH_LONG).show();
- //獲得SharedPreferences對(duì)象
- SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
- //播放器跳轉(zhuǎn)到上一次播放的進(jìn)度
- mPlayer.seekTo(preferences.getInt("CurrentPosition", 0));
- //開(kāi)始播放
- mPlayer.start();
- return null;
- }
- @Override
- public boolean onUnbind(Intent intent){
- Toast.makeText(getApplicationContext(), "Service onUnbind()", Toast.LENGTH_LONG).show();
- //獲得SharedPreferences對(duì)象
- SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
- Toast.makeText(getApplicationContext(), "CurrentPosition="+mPlayer.getCurrentPosition(), Toast.LENGTH_LONG).show();
- //獲得editor對(duì)象,寫(xiě)入一個(gè)整數(shù)到SharePreferences中,記住要用commit()提交,否則不會(huì)實(shí)現(xiàn)寫(xiě)入操作
- preferences.edit().putInt("CurrentPosition",mPlayer.getCurrentPosition()).commit();
- mPlayer.stop();
- return false;
- }
- }
復(fù)制代碼3、更改AndroidManifest.xml內(nèi)容如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest android:versionname="1.0" android:versioncode="1" xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson19">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:label="@string/app_name" android:name=".MainHelloSharedPreferences">
- <intent -filter="">
- <action android:name="android.intent.action.MAIN">
- <category android:name="android.intent.category.LAUNCHER">
- </category></action></intent>
- </activity>
- <service android:name=".MusicService" android:enabled="true">
- </service>
- </application>
- <uses android:minsdkversion="8" -sdk="">
- </uses></manifest>
復(fù)制代碼4、res/layout/mail.xml的內(nèi)容如下:
- <?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="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="SharedPreferences的使用" android:id="@+id/TextView01">
- </textview>
- <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="綁定音樂(lè)播放服務(wù)" android:id="@+id/Button01" android:layout_margintop="10dp">
- </button>
- <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="解綁定音樂(lè)播放服務(wù)" android:id="@+id/Button02" android:layout_margintop="10dp">
- </button>
- </linearlayout>
復(fù)制代碼5、MainHelloSharedPreferences.java的內(nèi)容如下:
- package android.basic.lesson19;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainHelloSharedPreferences extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //定義UI組件
- Button b1 = (Button) findViewById(R.id.Button01);
- Button b2 = (Button) findViewById(R.id.Button02);
- //定義ServiceConnection對(duì)象
- final ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- }
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
- //定義按鈕的單擊監(jiān)聽(tīng)器
- OnClickListener ocl = new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainHelloSharedPreferences.this,
- android.basic.lesson19.MusicService.class);
- switch (v.getId()) {
- case R.id.Button01:
- Toast.makeText(getApplicationContext(), "Button01 onClick", Toast.LENGTH_LONG).show();
- //綁定服務(wù)
- bindService(intent,conn,Context.BIND_AUTO_CREATE);
- break;
- case R.id.Button02:
- Toast.makeText(getApplicationContext(), "Button02 onClick", Toast.LENGTH_LONG).show();
- //取消綁定
- unbindService(conn);
- break;
- }
- }
- };
- //綁定單擊監(jiān)聽(tīng)器
- b1.setOnClickListener(ocl);
- b2.setOnClickListener(ocl);
- }
- }
復(fù)制代碼6、運(yùn)行程序,查看運(yùn)行情況:
查看 File Explorer,在/data/data/android.basic.lesson19/shared_prefs/目錄下有一個(gè)MusicCurrentPosition.xml文件,點(diǎn)擊右上角的按鈕 pull a file from the device,可以把這個(gè)xml文拷貝出來(lái)
7、查看MusicCurrentPosition.xml的內(nèi)容,可以看到音樂(lè)播放進(jìn)度的數(shù)據(jù)存貯在這個(gè)xml中
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <int name="CurrentPosition" value="15177">
- </int></map>
復(fù)制代碼興趣的同學(xué)可以嘗試一下,在Activity中增加一個(gè)按鈕,點(diǎn)擊以后把SharedPreference中的播放進(jìn)度數(shù)據(jù)取出來(lái),顯示在另一個(gè)文本框Textview02里,我在這里把最后的運(yùn)行結(jié)果圖放這里,代碼你們可以自己練習(xí)著敲出來(lái),從中體會(huì)Share的意思,是不是在同一個(gè)APK中不同的組件之間都可以去訪問(wèn)這個(gè)共享的持久化數(shù)據(jù)?從這一點(diǎn)上說(shuō)是不是有點(diǎn)像是Cookie?
二、Android中的文件IO操作(待續(xù))