Check if the app is turned on for the first time

I am new to Android development and I want to configure some application attributes based on the first application launch after installation. Is there a way to find that the application is running for the first time, and then configure its first launch attributes?

+70
android
Aug 27 2018-11-21T00:
source share
10 answers

The following is an example of using SharedPreferences to test the "first run".

 public class MyActivity extends Activity { SharedPreferences prefs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Perhaps set content view here prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (prefs.getBoolean("firstrun", true)) { // Do first run stuff here then set 'firstrun' as false // using the following line to edit/commit prefs prefs.edit().putBoolean("firstrun", false).commit(); } } } 

When the code starts prefs.getBoolean(...) , if a boolean with the "firstrun" key is not stored in SharedPreferences , this means that the application never started (since nothing ever saved a logical value with this key or the user cleared the application data, to force the "first run" script). If this is not the first run, then the line is prefs.edit().putBoolean("firstrun", false).commit(); will be executed and therefore prefs.getBoolean("firstrun", true) will actually return false, since it overrides the default value specified as the second parameter.

+186
Aug 27 2018-11-11T00:
source share

The accepted answer does not distinguish between the first launch and subsequent updates. Just setting logical in the general settings will only indicate if this is the first launch after the application was first installed. Later, if you want to update your application and make some changes to the first launch of this update, you will no longer be able to use this boolean, because the general settings are saved in updates.

This method uses general settings to save the version code, not the logical one.

 private void checkFirstRun() { final String PREFS_NAME = "MyPrefsFile"; final String PREF_VERSION_CODE_KEY = "version_code"; final int DOESNT_EXIST = -1; // Get current version code int currentVersionCode = BuildConfig.VERSION_CODE; // Get saved version code SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST); // Check for first run or upgrade if (currentVersionCode == savedVersionCode) { // This is just a normal run return; } else if (savedVersionCode == DOESNT_EXIST) { // TODO This is a new install (or the user cleared the shared preferences) } else if (currentVersionCode > savedVersionCode) { // TODO This is an upgrade } // Update the shared preferences with the current version code prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply(); } 

You will probably call this method from onCreate in your main action so that it checks every time your application starts.

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkFirstRun(); } private void checkFirstRun() { // ... } } 

If you need, you can configure the code to perform certain actions depending on which version the user has previously installed.

The idea came from this answer . They are also useful:

  • How can you get the manifest version number from the XML variables of the application (Layout)?
  • Custom version AndroidManifest.xml name value in code
+64
May 16 '15 at 10:07
source share
  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.UUID; import android.content.Context; public class Util { // =========================================================== // // =========================================================== private static final String INSTALLATION = "INSTALLATION"; public synchronized static boolean isFirstLaunch(Context context) { String sID = null; boolean launchFlag = false; if (sID == null) { File installation = new File(context.getFilesDir(), INSTALLATION); try { if (!installation.exists()) { launchFlag = true; writeInstallationFile(installation); } sID = readInstallationFile(installation); } catch (Exception e) { throw new RuntimeException(e); } } return launchFlag; } private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r");// read only mode byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); } private static void writeInstallationFile(File installation) throws IOException { FileOutputStream out = new FileOutputStream(installation); String id = UUID.randomUUID().toString(); out.write(id.getBytes()); out.close(); } } > Usage (in class extending android.app.Activity) Util.isFirstLaunch(this); 
+5
Jul 16 '13 at 5:45
source share

There is no way to find out through the Android API. You must save some flag yourself and make it persistent either in SharedPreferenceEditor or using a database.

If you want to use some license-related material on this flag, I suggest you use the obfuscation preference editor provided by the LVL library. It is simple and clean.

Regards, Stefan

+4
Aug 27 2018-11-22T00:
source share

Just check for some preferences the default value indicating that this is the first run. Therefore, if you get the default value, initialize and set this preference to a different value to indicate that the application is already initialized.

+3
Aug 27 2018-11-21T00:
source share

There is no reliable way to detect the first run, since the general preference method is not always safe, the user can delete the general settings data from the settings! the best way is to use the answers here Is there a unique identifier for the Android device? to get the unique identifier of the device and save it somewhere on your server, so whenever the user launches the application that you request on the server, and check if it is in your database or is it new.

+2
Apr 03 '15 at 20:16
source share

The following is an example of using SharedPreferences to check forWhat.

  preferences = PreferenceManager.getDefaultSharedPreferences(context); preferencesEditor = preferences.edit(); public static boolean isFirstRun(String forWhat) { if (preferences.getBoolean(forWhat, true)) { preferencesEditor.putBoolean(forWhat, false).commit(); return true; } else { return false; } } 
+2
Jul 26 '16 at 17:13
source share

It can help you.

 public class FirstActivity extends Activity { SharedPreferences sharedPreferences = null; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); sharedPreferences = getSharedPreferences("com.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (sharedPreferences.getBoolean("firstRun", true)) { //You can perform anything over here. This will call only first time editor = sharedPreferences.edit(); editor.putBoolean("firstRun", false) editor.commit(); } } } 
0
Mar 16 '16 at 7:16
source share
 SharedPreferences mPrefs; final String welcomeScreenShownPref = "welcomeScreenShown"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPrefs = PreferenceManager.getDefaultSharedPreferences(this); // second argument is the default to use if the preference can't be found Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); if (!welcomeScreenShown) { // here you can launch another activity if you like SharedPreferences.Editor editor = mPrefs.edit(); editor.putBoolean(welcomeScreenShownPref, true); editor.commit(); // Very important to save the preference } } 
0
May 19 '16 at 3:51
source share

I am not sure if this is a good way to test this. How about when the user uses the "clear data" button from the settings? SharedPreferences will be cleared and you will catch the β€œfirst run” again. And that is the problem. I assume it is better to use InstallReferrerReceiver.

0
Jan 12 '17 at 8:41
source share



All Articles