The oldest programmer Development Training 9-Android-Application Introduction page Implementation 1
In this section, we return to the Android end and discuss the implementation of the page when the application is running for the first time.
First, we need to know whether the application is running for the first time. Determine whether the application is running for the first time. Generally, the application persistently stores a specific information and checks whether the information exists at each startup. If not, the application is running for the first time. There are three methods for persistent storage of information on the Android platform: SharedPreferences, files, and SQLite databases. This is usually similar to whether an application runs such small information for the first time, it is generally stored in SharedPreferences.
In the controller package of the WkyLib library project, we first define a WkyApplication class whose base class is Application. In this function, we can determine whether the Application is running for the first time. In the Android system, the Application can be regarded as a Singleton class provided by the system in the Android system, and some information such as global variables can be saved.
The WkyApplication class code is as follows:
Public class WkyApplication extends Application {/*** query the values saved in SharedPreference and check whether the initial version * @ return * [yan Tao 2015.09.09] */public boolean isFirstRun () is run for the first time () {SharedPreferences pref = getSharedPreferences (S_PREF_NAME, MODE_PRIVATE); return pref. getBoolean (S_PREF_FIRST_RUN, true);}/*** checks whether the SharedPreference contains userId and other information, determine whether the user REGISTERS * @ return * [yan Tao 2015.09.09] initial version */public boolean isLogin () {return true;} public final static String S_PREF_NAME = WkgJys; public final static String S_PREF_FIRST_RUN = firstRun ;}The above code is relatively simple and does not need to be explained. However, we also want to emphasize that getSharedPreferences uses pre-defined constants instead of writing strings directly, so that the value cannot be found due to spelling errors first, at the same time, it increases the readability of the Code. Although it is nothing, good programming habits are very important. If you do not pay attention to it, you will have to pay a heavy price sooner or later.
In the WkgJys project of a specific application, define the JysApplication class inherited from WkyApplication in the controller package. The Code is as follows:
public class JysApplication extends WkyApplication {}To enable this class to work, add the android: name attribute to the Manifest file of the WkgJys project:
Finally, on the Splash page, check whether the application is running for the first time at the scheduled end. When the first operation is performed, start the application introduction page. The Code is as follows:
StartMainActivityTask = new Runnable () {@ Override public void run () {if (jysApplication. isFirstRun () {Log. e (wky, show introduction page);} else if (! JysApplication. isLogin () {Log. e (wky, start the logon page);} else {Intent intent = new Intent (SplashActivity. this, MainActivity. class); startActivity (intent) ;}finish ();}};