Android - 如何才能知道这是否是第一次启动应用程序?

ofe*_*109 4 methods android launching-application

如何知道这是否是第一次启动应用程序?

如果您要回答,请添加完整的代码,因为我已经阅读了一些答案而且我不理解它们.

谢谢.

Pro*_*les 17

每个应用程序都有一种存储首选项或选项的方法,因此您可以使用该应用程序来确定该应用程序之前是否已运行

SharedPreferences runCheck = PreferenceManager.getSharedPreferences("hasRunBefore", 0); //load the preferences
Boolean hasRun = runCheck.getBoolean("hasRun", false); //see if it's run before, default no
if (!hasRun) {
    SharedPreferences settings = getSharedPreferences("hasRunBefore", 0);
    SharedPreferences.Editor edit = settings.edit();
    edit.putBoolean("hasRun", true); //set to has run
    edit.commit(); //apply
    //code for if this is the first time the app has run
}
else {
    //code if the app HAS run before
}
Run Code Online (Sandbox Code Playgroud)


jai*_*nal 11

使用sharedPreferences的持久性数据storage.when首次推出只保存在共享Preferences.Then一个布尔值,每次检查应用程序.

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("isLauncedTime",true);
prefEditor.commit();
Run Code Online (Sandbox Code Playgroud)

  • 我有下面的代码 (5认同)

jcx*_*ier 5

您可能需要参考http://developer.android.com/guide/topics/data/data-storage.html#pref.

当您第一次执行应用程序到共享首选项时,您可以加载一个布尔值,然后在以下运行中检查它是否为true,因此您知道该程序已经运行过一次.