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

发布于 2024-12-23 01:22:59 字数 85 浏览 1 评论 0原文

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

如果您正在回答,请添加完整的代码,因为我已经阅读了一些答案,但我不明白它们。

谢谢。

How can I know if it is the first time the application launched?

If you are answering please add a full code because I have read some answers and I didn't understand them.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

各自安好 2024-12-30 01:22:59

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

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
}

Every app gets a way to store preferences or options, so you can have one for whether or not the app has previously run

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
}
陈独秀 2024-12-30 01:22:59

使用sharedPreferences进行持久数据存储。应用程序首次启动时只需在共享首选项中保存一个布尔值。然后每次都进行检查。

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("isLauncedTime",true);
prefEditor.commit();

Use sharedPreferences for the persistent data storage.when the application first launched just save a boolean value in the shared Preferences.Then check each time.

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("isLauncedTime",true);
prefEditor.commit();
柳若烟 2024-12-30 01:22:59

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

您可以在第一次执行应用程序时加载一个布尔值到共享首选项,然后在接下来的运行中检查它是否为真,这样您就知道该程序已经运行过一次。

You might want to refer to http://developer.android.com/guide/topics/data/data-storage.html#pref.

You can load a boolean when you execute your application for the first time to the shared preferences, and then check if it true in following runs, so you know the program has already been run once.

始终不够 2024-12-30 01:22:59

我不知道这是否是最好的解决方案,但是......

我尝试在Android的文件系统中存储一个简单的布尔值,并在启动Android应用程序时检查该布尔值是否存在以及它的值是什么。

再次强调,不确定这是否是正确的方法,这只是我自己解决这个问题的方法。

I don't know if this is the best solution, but...

I tried storing a simple boolean in the Android's file system and when starting the android app, checking if that boolean exists and what it's value is.

Again, not sure if this is the proper way to do it, it's just my own way of getting around this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文