Android应用程序生命周期和服务

发布于 2024-09-19 19:27:47 字数 552 浏览 4 评论 0原文

也许这不是一个非常复杂的问题,但首先,我不知道我应该搜索什么搜索查询?!

在我的应用程序开始时,我想启动 GPS,如果我的应用程序结束,则应关闭 GPS。

如何检查整个应用程序(而不是活动)是否已完成?

对于永远不会用 finish() 关闭的 Start-Activity 使用 onDestroy-Method 是否足够?

非常感谢您,并对初学者的问题表示歉意。

Mur

UPD

我看到了第一个答案,我想说一次。

我的意思不是活动,而是整个应用程序(其中存在许多活动)。

如何检查所有应用程序的活动是否已完成,并且只有在这种情况下才停止服务?

有可能吗?

UPD2:

我已经在设备上测试了我的解决方案:

“对于 Start-Activity 使用 onDestroy-Method 是否足够,它永远不会用 finish() 关闭?”

是的,已经足够了。

Probably it's not a really complicated question, but first of all, I have no idea, what search query should I search for?!

At the beginning of my application I would like to start GPS and if my application will be enden GPS should be closed.

How can I check, if the whole Application (not an Activity) is finished?

Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?

Thank you very much and sorry for a beginner's question.

Mur

UPD

I saw the first answer and I'd like to say once.

I don't mean an ACTIVITY, I mean really the whole APPLICATION (in which many activities exist).

How to check if all application's activities were finished and only in that case stop the service?

Is there posibility for that?

UPD2:

I've tested my solution on a device:

"Is it enough to use onDestroy-Method for Start-Activity, which will never closed with finish()?"

Yes, it was enough.

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

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

发布评论

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

评论(2

黑寡妇 2024-09-26 19:27:47

使每个 Activity 都与 Service 绑定(通过 bindService)...当所有 Activity 都终止(隐式解除绑定)时,您的 Service 将消失。因为只要有人与其绑定,服务就会一直存在。

Make it so that, each Activity binds (via bindService) with the Service...When all the activities have been terminated (unbinds implicitly), your Service will perish. Since the Service will remain alive as long as someone is binding with it.

我早已燃尽 2024-09-26 19:27:47

在这种特殊情况下,您需要做的是:

  • 创建一个 Activity 来向用户显示信息。
  • 创建一个将在后台运行的 Service 并将更新发送到 Activity

有很多您想要做的示例,但基本上您可以启动 <在 ActivityonStart() 方法上调用 code>Service 并在 onDestroy() 上结束服务。

In this particular case what you need to do is:

  • Create an Activity to show information to the user.
  • Create a Service that will run on background and will send the updates to the Activity

There are lot's of examples of what you are trying to do, but basically you can start the Service on the onStart() method of your Activity and ending the service on the onDestroy().

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