Android开发中,我们经常会用到SharedPreferences,它是一种轻量的数据存储方式,通常用来存储一些简单的配置信息(首次登陆 记住密码)
package com.example.day09_sp01;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取sharedperfrence对象
preferences = getSharedPreferences("name", MODE_PRIVATE);
//取值
boolean flag = preferences.getBoolean("flag", false);
if(flag){
Intent intent=new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
finish();
}
}
//点击存值
public void click(View view){
//获取编辑器
Editor edit = preferences.edit();
//存值
edit.putBoolean("flag", true);
//提交
edit.commit();
Intent intent=new Intent(MainActivity.this, ShowActivity.class);
startActivity(intent);
finish();
}
}