Android SharedPreference的用法及获得系统当前时间
Android SharedPreference的用法及获得系统当前时间
Android SharedPreference的用法及获得系统当前时间
- package lxy.litsoft;
- import java.sql.Date;
- import java.text.SimpleDateFormat;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- public class AppMain extends Activity {
- private String currentTime;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- protected void onResume() {
- super.onResume();
- //读取数据信息
- //1.获取Preferences
- SharedPreferences prefrence = getSharedPreferences("configuration", 0);
- //2.取出数据,如果没有存过数据,则读出的数据为"the fist time"这个参数
- String date = prefrence.getString("date","the fist time");
- Log.d("test", date);
- Toast.makeText(AppMain.this, "Last time you login is "+date, Toast.LENGTH_LONG).show();
- }
- protected void onPause() {
- super.onPause();
- /** 获得系统当前时间 **/
- SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
- Date curDate = new Date(System.currentTimeMillis());//获取当前时间
- currentTime = formatter.format(curDate);
- /** 存放数据信息 **/
- //1.打开Preferences,名称为configuration,如果存在则打开它,否则创建新的Preferences
- SharedPreferences prefrence = getSharedPreferences("configuration", 0);
- //2.让configuration处于编辑状态
- SharedPreferences.Editor editor = prefrence.edit();
- //3.存放数据
- editor.putString("date",currentTime);
- //4.完成提交
- editor.commit();
- Toast.makeText(AppMain.this, "ByeBye!!", Toast.LENGTH_LONG).show();
- }
- }
评论暂时关闭