信息发布→ 登录 注册 退出

Android 清除SharedPreferences 产生的数据(实例代码)

发布时间:2026-01-11

点击量:

复制代码 代码如下:

 定义:
        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);
  // 提交保存的结果
  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中清除。 值得注意的地方是清除的时候也要提交,不然的话,数据没刷新还是保持原来的数据,刚开始没有注意到清除也要commit。搞得我郁闷。。。
 protected void onDestroy() {
  // TODO Auto-generated method stub

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

在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!