Android 服务:生命周期注意事项

发布于 2024-11-28 04:48:45 字数 274 浏览 1 评论 0原文

我正在制作一个 Android 应用程序,它将有两个服务,每 24 小时发送一次有关用户使用手机的数据。

用户应该执行该应用程序,切换按钮以启用手机使用情况的记录,然后用户应该能够使用手机进行正常生活,直到他再次启动该应用程序并禁用切换按钮以停止该应用程序。记录信息。

  • 关于服务的生命周期我应该考虑哪些因素?
  • 当服务发送数据时,用户与手机的交互怎么样?

非常感谢所有信息,因为我的思想对这一切有点不知所措!

提前非常感谢大家!

I am making an android app which will have two services that will keep sending data about the usage of the phone by the user every 24 hours.

The user should execute the app, toggle the buttons to enable the logging of the usage of the phone and then the user should be able to do a normal life with his phone, until he starts again the app and disables the toggle button to stop the logging of the info.

  • What considerations should I take about the life cycle of the services?
  • What about the interaction of the user with the phone while the services should be sending the data?

All info is very much appreciated, as I my mind is getting a little bit overwhelmed with all this!

Thanks a lot in advance everybody!

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

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

发布评论

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

评论(1

夜灵血窟げ 2024-12-05 04:48:45

通过设置菜单可以随时中断该服务。如果 Android 决定当前正在运行的活动需要资源,它也可以随时被杀死。不管怎样,onDestroy()都会被调用,所以用它来存储需要的任何东西。

该服务在后台运行,但通过主 UI 线程运行。因此,可以通过服务阻止电话的操作。当它实际上是一项试图做某事的服务时,手机看起来像是被锁定了。任何阻塞过程都应该在线程中使用,例如 Java 计时器、Java 线程或 AsyncTask。

在任何给定时间只能有一个正在运行的服务版本。但是,如果“myService”已在运行,则调用 startService(myService) 本质上会覆盖当前正在运行的服务,并且 onStartCommand() 将被再次调用。但是,无论调用多少次 startService(myService) ,都需要调用一次 stopService(myService) 来停止它。

如果服务绑定到任何东西,stopService(myService) 将不会停止服务。它将等到所有绑定都被删除后,服务才会停止。

The service can be cut at any time through the settings menu. It can also be killed at any time by Android if it decides it needs the resources for the currently running activity. onDestroy() will be called regardless so use that to store anything needed.

The service runs in the background but through the main UI thread. Thus, it is possible to block operation of the phone through a service. It looks like the phone locked up when it's really a service trying to do something. Any blocking procedure should be used in a thread such as Java timer, Java thread, or AsyncTask.

There can only be one running version of the service at any given time. However, calling startService(myService) if "myService" is already running will essentially override the current running service and onStartCommand() will be called again. However, one call to stopService(myService) is needed to stop it no matter how many times startService(myService) was called.

stopService(myService) will not stop a service if the service is bound to anything. It will wait until all bindings are removed before the service stops.

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