I. Basic Knowledge:
Preferences is a lightweight database storage mechanism used to record game scores and the last login time of applications.
Ii. Programming implementation:
1. Edit the interface (res \ layout \ main. xml ):
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "25dip"
Android: id = "@ + id/TextView01"
/> <! -- Add TextView -->
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "25dip"
Android: id = "@ + id/TextView01"
/> <! -- Add TextView -->
</LinearLayout>
2. Edit the code (\ src \ wyf \ zcl \ MyActivity. java ):
[Java]
Package wyf. zcl;
Import java. util. Date; // introduce related packages
Import android. app. Activity; // introduce related packages
Import android. content. Context; // introduce related packages
Import android. content. SharedPreferences; // introduce related packages
Import android. OS. Bundle; // introduce related packages
Import android. widget. TextView; // introduce related packages
Import android. widget. Toast; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SharedPreferences sp = this. getSharedPreferences ("preset pre", Context. MODE_PRIVATE );
// Return a SharedPreferences instance. The first parameter is the Preferences name, and the second parameter is the default operation.
String lastLogin = sp. getString (// time when the last access was read from SharedPreferences
"Ll", // key value
Null // Default Value
);
If (lastLogin = null ){
LastLogin = "Welcome, this is your first visit to this Preferences ";
} Else {
LastLogin = "welcome back. You logged on to" + lastLogin + "last time ";
}
// Write back the access time to SharedPreferences
SharedPreferences. Editor editor = sp. edit ();
Editor. putString ("ll", new Date (). toLocaleString (); // Add the current time to the editor
Editor. commit (); // submit editor
TV = (TextView) this. findViewById (R. id. TextView01 );
TV. setText (lastLogin );
}
}
Package wyf. zcl;
Import java. util. Date; // introduce related packages
Import android. app. Activity; // introduce related packages
Import android. content. Context; // introduce related packages
Import android. content. SharedPreferences; // introduce related packages
Import android. OS. Bundle; // introduce related packages
Import android. widget. TextView; // introduce related packages
Import android. widget. Toast; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SharedPreferences sp = this. getSharedPreferences ("preset pre", Context. MODE_PRIVATE );
// Return a SharedPreferences instance. The first parameter is the Preferences name, and the second parameter is the default operation.
String lastLogin = sp. getString (// time when the last access was read from SharedPreferences
"Ll", // key value
Null // Default Value
);
If (lastLogin = null ){
LastLogin = "Welcome, this is your first visit to this Preferences ";
} Else {
LastLogin = "welcome back. You logged on to" + lastLogin + "last time ";
}
// Write back the access time to SharedPreferences
SharedPreferences. Editor editor = sp. edit ();
Editor. putString ("ll", new Date (). toLocaleString (); // Add the current time to the editor
Editor. commit (); // submit editor
TV = (TextView) this. findViewById (R. id. TextView01 );
TV. setText (lastLogin );
}
}
3. Running effect: