SharedPreferences存取应用程序访问时间
package com.example.sharepre1; import java.util.Date; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private TextView showtime; private static final String FILENAME = "time"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showtime = (TextView) findViewById(R.id.showtime); SharedPreferences preferences = getSharedPreferences(FILENAME,0); String lasttime = preferences.getString("time", null); if(lasttime==null){ lasttime = "第一次使用本应用程序"; }else{ lasttime = "你上次访问的时间为"+lasttime; } Editor editor = preferences.edit(); editor.putString("time", new Date().toLocaleString()); editor.commit(); showtime.setText(lasttime); } }