记录一个问题,更改sharepreference的值后立刻关机

现象:
更改sharepreference的值(一开始是3,置为2)后直接关机,重新开机后查询,值为2,启动app后,值被回滚到3

7.13找到问题了,重启设备之后,启动app之前,查看了SharedPreferences存储的文件夹(SharedPreferences文件都是存放在/data/data//shared_prefs/目录下的),发现文件目录下,除了存储的SharedPreferences,还多了一个.bak文件。
根据这个搜索找到一篇博文

https://www.jianshu.com/p/2a4b411383d4

启动app后,调用SharedPreferences时,应用首先检测.bak备份文件是否存在,如果存在的话,那么则将原来的文件删除,然后将.bak文件renameto正常文件,也就意味着,如果在写的时候,出问题了,导致中断了,就使用原来没问题的备份文件。

package com.example.pc.testeverything.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.pc.testeverything.MyApplication;
import com.example.pc.testeverything.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by PC on 2018/7/13.
 */

public class TestLayoutActivity extends AppCompatActivity {
    final static String SP_SET_CONFIG = "config";
    public static final String CURRENT_MODEL = "current_work";
    private SharedPreferences sp;
    private boolean click=false;
    private Button button;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_two_constraintlayout);
     /*   sp = this.getSharedPreferences(SP_SET_CONFIG, Context.MODE_PRIVATE);*/
        sp=PreferenceManager.getDefaultSharedPreferences(this);
     /*   sp=this.getPreferences(MODE_MULTI_PROCESS);*/
        Log.i("当前值:",""+String.valueOf(sp.getInt(CURRENT_MODEL,6)));
        button= (Button) findViewById(R.id.img1);
        button.setText(String.valueOf(sp.getInt(CURRENT_MODEL,6)));
        //设置字体
        button.setTypeface(MyApplication.getTypefaceCaiYun());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (click){
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt(CURRENT_MODEL, 3);
                    editor.apply();
                    button.setText(String.valueOf(sp.getInt(CURRENT_MODEL,6)));
                    Log.i("当前值:",""+String.valueOf(sp.getInt(CURRENT_MODEL,6)));
                    click=false;
                }else{
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putInt(CURRENT_MODEL, 2);
                    editor.apply();
                    button.setText(String.valueOf(sp.getInt(CURRENT_MODEL,6)));
                    Log.i("当前值:",""+String.valueOf(sp.getInt(CURRENT_MODEL,6)));
                    click=true;
                }

            }
        });
/*        setContentView(R.layout.test_constraintlayout);*/
    }
}

猜你喜欢

转载自blog.csdn.net/changhuzichangchang/article/details/81033458