Sharepreference
用于存储始终不怎么变化的数据值,如手机的设置等信息,只会采用已经存在的配置,而不会发生较大变化的值
使用Sharepreferences时需要创建他自身的对象还要创建SharedPreferences.Editor(此对象用于数据的写入),写入方法Editor对象的putString等方法存入key-value值,获取的时候用Sharepreference对象的getString(key)等方法获取存入的数据
方法
getXxx(String key,xxx defValue)
获取相关key的值,其中defvalue是当value值不存在时,返回默认值defvalue
Editor.putXxx(String key,xxx value)
以key-value的形式存放数据
Editor.apply()或者Editor.commit()
用于提交Editor的修改
Editor.remove(String key)
删除Sharepreference中指定key对应的数据
boolean contains(String key)
判断key是否存在
Editor.clear()
清楚所有数据
getSharedPreferences(String name,int mode)
mode有三个值
Context.MODE_PRIVATE 指定该SharedPreferences数据只能被本应用程序读写
其他的两种不推荐:
Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写
Context.MODE_WORLD_WRITEABLE:指定SharedPreferences数据能被其他应用程序读写.
public class SharePreferencesActivity extends Activity {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
private Context context = SharePreferencesActivity.this;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shareprefences);
preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
editor = preferences.edit();
Button btn = findViewById(R.id.insert);
Button btn_write = findViewById(R.id.write);
Button btn_deleteAll =findViewById(R.id.deleteAll);
btn_deleteAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog alterDialog = new AlertDialog.Builder(context)
.setTitle("确认框")
.setMessage("您确定要清除存的数据吗")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
editor.clear();
editor.commit();
Toast.makeText(context,"删除成功",Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(context,"取消成功",Toast.LENGTH_LONG).show();
}
}).show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String time = preferences.getString("time",null);
int randNum =preferences.getInt("random",0);
String result = time == null ? "您暂时还未写入数据" : "写入时间:" + time + "\n 上次生成的随机数" +randNum;
Toast.makeText(SharePreferencesActivity.this,result,Toast.LENGTH_LONG).show();
}
});
btn_write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
editor.putString("time",sdf.format(new Date()));
editor.putInt("random",(int) (Math.random()*100));
editor.apply();
}
});
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据"
android:id="@+id/insert"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入数据"
android:id="@+id/write"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除数据"
android:id="@+id/deleteAll"/>
</LinearLayout>