Android determines whether the software is running for the first time

Source: Internet
Author: User

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.

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.