Android determines whether the software is running for the first time
This article describes how android judges whether the software is running for the first time. Share it with you for your reference. The details are as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11
12 |
SharedPreferences sharedPreferences = this. getSharedPreferences ("share", MODE_PRIVATE );
Boolean isFirstRun = sharedPreferences. getBoolean ("isFirstRun", true );
Editor editor = sharedPreferences. edit ();
If (isFirstRun)
{
Log. d ("debug", "First Run ");
Editor. putBoolean ("isFirstRun", false );
Editor. commit ();
} Else
{
Log. d ("debug", "not the first run ");
}
|
At the same time, if we need to save some variables in the memory, we can use SharedPreferences as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11 |
SharedPreferences sharedPreferences = this. getSharedPreferences ("share", MODE_PRIVATE );
// This indicates that the application is dedicated
Boolean isFirstRun = sharedPreferences. getBoolean ("isFirstRun", true );
// This indicates that if the value corresponding to key "isFirstRun" does not have a value, the default value is true,
// Otherwise, the corresponding value is taken out and assigned to the variable isFirstRun.
Editor editor = sharedPreferences. edit ();
Editor. putBoolean ("isFirstRun", false );
// PutBoolean (key, value ),
// Write the value to the corresponding key, which is one-to-one
Editor. commit ();
// Write isFirstRun to the editor and save it
|
Note: In this case, the isFirstRun value persists unless data is cleared or uninstalled in the background.
I hope this article will help you design your Android program.