国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
第十八講:Android SharedPreferences和File
本講內(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,代碼如下:
  1. package android.basic.lesson19;

  2. import android.app.Service;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.media.MediaPlayer;
  7. import android.os.IBinder;
  8. import android.widget.Toast;

  9. public class MusicService extends Service {

  10.         //定義MediaPlayer播放器變量
  11.         MediaPlayer mPlayer = new MediaPlayer();

  12.         @Override
  13.         public void onCreate() {
  14.                 Toast.makeText(getApplicationContext(), "Service onCreate()", Toast.LENGTH_LONG).show();
  15.                 //創(chuàng)建播放器
  16.                 mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.babayetu);
  17.                 //設(shè)置自動(dòng)循環(huán)
  18.                 mPlayer.setLooping(true);
  19.         }

  20.         @Override
  21.         public IBinder onBind(Intent intent) {
  22.                 Toast.makeText(getApplicationContext(), "Service onBind()", Toast.LENGTH_LONG).show();
  23.                 //獲得SharedPreferences對(duì)象
  24.                 SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
  25.                 //播放器跳轉(zhuǎn)到上一次播放的進(jìn)度
  26.                 mPlayer.seekTo(preferences.getInt("CurrentPosition", 0));
  27.                 //開(kāi)始播放
  28.                 mPlayer.start();
  29.                 return null;
  30.         }

  31.         @Override
  32.         public boolean onUnbind(Intent intent){
  33.                 Toast.makeText(getApplicationContext(), "Service onUnbind()", Toast.LENGTH_LONG).show();
  34.                 //獲得SharedPreferences對(duì)象
  35.                 SharedPreferences preferences = this.getSharedPreferences("MusicCurrentPosition",Context.MODE_WORLD_WRITEABLE);
  36.                 Toast.makeText(getApplicationContext(), "CurrentPosition="+mPlayer.getCurrentPosition(), Toast.LENGTH_LONG).show();
  37.                 //獲得editor對(duì)象,寫(xiě)入一個(gè)整數(shù)到SharePreferences中,記住要用commit()提交,否則不會(huì)實(shí)現(xiàn)寫(xiě)入操作
  38.                 preferences.edit().putInt("CurrentPosition",mPlayer.getCurrentPosition()).commit();
  39.                 mPlayer.stop();
  40.                 return false;
  41.         }
  42. }
復(fù)制代碼
3、更改AndroidManifest.xml內(nèi)容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest android:versionname="1.0" android:versioncode="1" xmlns:android="http://schemas.android.com/apk/res/android" package="android.basic.lesson19">
  3.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  4.         <activity android:label="@string/app_name" android:name=".MainHelloSharedPreferences">
  5.             <intent -filter="">
  6.                 <action android:name="android.intent.action.MAIN">
  7.                 <category android:name="android.intent.category.LAUNCHER">
  8.             </category></action></intent>
  9.         </activity>
  10.             <service android:name=".MusicService" android:enabled="true">
  11.             </service>
  12.     </application>
  13.     <uses android:minsdkversion="8" -sdk="">

  14. </uses></manifest>
復(fù)制代碼
4、res/layout/mail.xml的內(nèi)容如下:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  3.         <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:text="SharedPreferences的使用" android:id="@+id/TextView01">
  4.         </textview>

  5. <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">
  6. </button>
  7. <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">
  8. </button>
  9. </linearlayout>
復(fù)制代碼
5、MainHelloSharedPreferences.java的內(nèi)容如下:
  1. package android.basic.lesson19;

  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.Toast;

  13. public class MainHelloSharedPreferences extends Activity {
  14.         /** Called when the activity is first created. */
  15.         @Override
  16.         public void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 setContentView(R.layout.main);

  19.                 //定義UI組件
  20.                 Button b1 = (Button) findViewById(R.id.Button01);
  21.                 Button b2 = (Button) findViewById(R.id.Button02);

  22.                 //定義ServiceConnection對(duì)象
  23.                 final ServiceConnection conn = new ServiceConnection() {

  24.                         @Override
  25.                         public void onServiceConnected(ComponentName name, IBinder service) {
  26.                         }

  27.                         @Override
  28.                         public void onServiceDisconnected(ComponentName name) {
  29.                         }
  30.                 };

  31.                 //定義按鈕的單擊監(jiān)聽(tīng)器
  32.                 OnClickListener ocl = new OnClickListener() {
  33.                         @Override
  34.                         public void onClick(View v) {
  35.                                 Intent intent = new Intent(MainHelloSharedPreferences.this,
  36.                                                 android.basic.lesson19.MusicService.class);
  37.                                 switch (v.getId()) {
  38.                                 case R.id.Button01:
  39.                                         Toast.makeText(getApplicationContext(), "Button01 onClick", Toast.LENGTH_LONG).show();
  40.                                         //綁定服務(wù)
  41.                                         bindService(intent,conn,Context.BIND_AUTO_CREATE);
  42.                                         break;
  43.                                 case R.id.Button02:
  44.                                         Toast.makeText(getApplicationContext(), "Button02 onClick", Toast.LENGTH_LONG).show();
  45.                                         //取消綁定
  46.                                         unbindService(conn);
  47.                                         break;
  48.                                 }
  49.                         }
  50.                 };

  51.                 //綁定單擊監(jiān)聽(tīng)器
  52.                 b1.setOnClickListener(ocl);
  53.                 b2.setOnClickListener(ocl);

  54.         }
  55. }
復(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中
  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
  2. <map>
  3. <int name="CurrentPosition" value="15177">
  4. </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ù))

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android網(wǎng)絡(luò)收音機(jī)項(xiàng)目
Android Service Tutorial
Android 廣播接受者
編寫(xiě)安卓無(wú)界面后臺(tái)程序 開(kāi)機(jī)自啟動(dòng)服務(wù)
activity、service、BroadcastReceive之間如何互相通訊,并取回相應(yīng)的結(jié)果
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服