如何让我的整个应用程序在后台运行?

发布于 2024-11-07 17:41:37 字数 216 浏览 0 评论 0原文

我制作了一个应用程序,它只是一个简单的计数器,可以跟踪用户打高尔夫球时的击球情况并在打高尔夫球时保存分数。我只是希望当用户退出时应用程序不会被杀死。我只是想让它在后台运行。因此,用户可以在做其他事情(例如发短信或检查电子邮件)后重新进入程序

我知道我需要使用服务,但在尝试在网络和其他论坛上研究该主题时,似乎他们都解释了如何让应用程序中的特定活动继续,而不是整个应用程序。

我该怎么做?

I made an app that is just a simple counter that will keep track of a user's strokes as they play golf and save their score while doing it. I simply want the app to not be killed when the user exits out. I just want it to run in the background. So the user can re-enter the program after doing other things, (like texting or checking email)

I understand I need to use services but while trying to research the topic on the web and in other forums, it seems that they all explain how to have a specific activity in the app to continue, not the entire app.

How might I do this?

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

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

发布评论

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

评论(4

一袭白衣梦中忆 2024-11-14 17:41:37

你可以做这样的事情

  @Override
  protected void onPause()
  {
    super.onPause();

    SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("score", the_users_score);
  }

并通过做得到分数

  @Override
  protected void onResume()
  {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
    the_users_score = settings.getString("score", "0");
  }

You could do something like this

  @Override
  protected void onPause()
  {
    super.onPause();

    SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("score", the_users_score);
  }

and get the score back by doing

  @Override
  protected void onResume()
  {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
    the_users_score = settings.getString("score", "0");
  }
胡大本事 2024-11-14 17:41:37

您应该将数据存储到永久存储中,而不是尝试无限期地使应用程序保持活动状态(无论如何都不能 100% 实现):

http://developer.android.com/guide/topics/data/data-storage.html

You should just store data to permanent storage instead of trying to keep the app alive indefinitely (which can not be 100% achieved anyway):

http://developer.android.com/guide/topics/data/data-storage.html

很糊涂小朋友 2024-11-14 17:41:37

我想指出 Ben Williams 基本上是正确的,但他在保存文件中错过了一个步骤 (editor.commit();)。在提交之前,数据不会存储在设置文件中:

@Override
protected void onPause()
  {
     super.onPause();

     SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
     SharedPreferences.Editor editor = settings.edit();
     editor.putString("score", the_users_score);
     editor.commit();
  }

我建议查看 http://developer.android.com/reference/android/app/Activity.html 查看生命周期(确切地知道在什么情况下你想把它放在哪里)。

I would like to point out that Ben Williams was mostly correct, but he missed a step (editor.commit();) in the save file. The data does not get stored in the settings file until it is committed:

@Override
protected void onPause()
  {
     super.onPause();

     SharedPreferences settings = getSharedPreferences("YourOwnPickedName", 0);
     SharedPreferences.Editor editor = settings.edit();
     editor.putString("score", the_users_score);
     editor.commit();
  }

I suggest looking at http://developer.android.com/reference/android/app/Activity.html to see the lifecycle (to know exactly where you want to put it for what situation).

萌︼了一个春 2024-11-14 17:41:37

您可以将其作为 Windows 服务应用程序。

You can make it as a windows service application.

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