欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實(shí)例代碼)

 更新時(shí)間:2013年11月02日 09:10:44   作者:  
項(xiàng)目是要保存上次文件播放的位置,我使用SharedPreferences來保存,鍵值對(duì)分別是文件路徑和當(dāng)時(shí)播放的位置

復(fù)制代碼 代碼如下:


 定義:
        SharedPreferences preferences = null;
 SharedPreferences.Editor editor = null;

  preferences = getSharedPreferences(TAG, Activity.MODE_PRIVATE);
  editor = preferences.edit();
在onstop里面保存播放位置
 @Override
 protected void onStop() {
  editor.putInt(filePath, currentposition);
  // 提交保存的結(jié)果
  Log.e(TAG, "onStop");
  editor.commit();
  super.onStop();
 }
在onPrepared中seekto到原來位置
public void onPrepared(MediaPlayer mp) {

 currentposition = preferences.getInt(filePath, -1);
  if (currentposition != -1) {
   mUvv.seekTo(currentposition);
   }

后面的話,我需要的是清除這些記錄,在ondestroy中清除。 值得注意的地方是清除的時(shí)候也要提交,不然的話,數(shù)據(jù)沒刷新還是保持原來的數(shù)據(jù),剛開始沒有注意到清除也要commit。搞得我郁悶。。。
 protected void onDestroy() {
  // TODO Auto-generated method stub

  if (clearshared) {
   editor.clear();
   editor.commit();
  }
  super.onDestroy();
 }

相關(guān)文章

最新評(píng)論