Checks whether the application is opened for the first time.

Source: Internet
Author: User

Checks whether the application is opened for the first time.

In many cases, we need to check whether the user opens the application for the first time to initialize some data or open the boot interface.


/* Method 1: * check whether a file exists (com. example. test. isFirstIn) * If yes, it indicates that * is not opened for the first time and * does not exist for the first time. After opening */File dir = getFilesDir (), the File is created (); /// data/com. example. test/filesFile f = new File (dir, "com. example. test. isFirstIn "); Log. e ("miquan", f. getAbsolutePath (); if (f. exists () {// dosomethingnew AlertDialog. builder (this ). setMessage ("You opened it n times "). show ();} else {try {f. createNewFile ();} catch (IOException e) {e. printStackTrace ();} // dosomethingnew AlertDialog. builder (this ). setMessage ("this is the first time "). show ();}

/* Method 2: * obtain the isFirstIn value when you open it for the first time. The default value is false * to obtain false. It indicates that * is not enabled for the first time and * is enabled for the first time; set the isFirstIn value to false **/SharedPreferences sp = getSharedPreferences ("isFirstIn", Activity. MODE_PRIVATE); boolean isFirstIn = sp. getBoolean ("isFirstIn", true); if (isFirstIn) {SharedPreferences. editor editor = sp. edit (); editor. putBoolean ("isFirstIn", false); editor. commit (); new AlertDialog. builder (this ). setMessage ("this is the first time "). show ();} else {new AlertDialog. builder (this ). setMessage ("You opened it n times "). show ();}



How does one determine that the user is running my program for the first time?

You can use a flag. For example, if you generate a flag file in a directory on an SD card during the first use, you can open the file later to determine if the file is not in use. If the file does not exist, you can use it for the first time, yes.
 
Android determines the first time a user logs on to the application

Simpler storage of sharedpreferences
Sqlite does not need to determine whether it exists by yourself. It is encapsulated.
Call getWritableDatabase ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.