是否可以在sharedPreferences中存储一段时间的值?

时间:2017-07-03 12:25:33

标签: android sharedpreferences

我想创建一种用户登录的会话,并且每次打开应用程序时都不必输入密码。

我试图做这样的事情,只是为了测试: 但如果您有任何想法,我真的很感激,谢谢。 同样,这只是一个测试,看看我是否可以在一段时间后修改存储的值。

public class MainActivity extends AppCompatActivity {


public SharedPreferences mPreferenceManager;
public SharedPreferences.Editor mEditor;
private static final int DURATION = 20000;
private long activatedAt = Long.MAX_VALUE;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView hello=(TextView) findViewById(R.id.hello);
    Button but1=(Button) findViewById(R.id.button1);


    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(MainActivity.this,configActivity.class);
            startActivity(i);
        }
    });

    SharedPreferences setting = getSharedPreferences("settings", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = setting.edit();


    long activeFor=System.currentTimeMillis()-activatedAt;
    if(activeFor>5000){
        editor.clear();
        Log.d("here-",">50000");
    }else{
        editor.putString("pseudo","hello");
        editor.commit();
        Log.d("here-","<50000");
    }
    hello.setText(setting.getString("pseudo",""));

}

0 个答案:

没有答案
相关问题