在前台启动两个服务? [英] Start two services in foreground?

查看:271
本文介绍了在前台启动两个服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在努力保持我的服务还活着,我可以有多个服务更加前景?

我一次启动两项服务,我不希望任何人被销毁。所以,在阅读了几个小时后,我找到了解决办法是返回的 START_STICKY ,并使用<一个href=\"http://developer.android.com/intl/ko/reference/android/app/Service.html#startForeground%28int,%20android.app.Notification%29\"相对=nofollow> startForeground(...) 我的服务。但是,我有两个服务,这样我就可以有他们两个是在前台,都不会被破坏?

I am starting two services at once, and I don't want any of them to be destroyed. So, after reading for hours, I found the solution is to return START_STICKY and use startForeground(...) on my service. But, I have two services, so can I have them both be in the foreground, and both never be destroyed?

推荐答案

有几个方面需要考虑。

There are several aspects to consider.


  1. 首先:如果你在前台两个服务,你必须有两则通知也是如此。这被认为是一种安全功能(检查例如这个帖子)。有在一个应用程序状态栏有两个图标可能会相当混乱给用户,并是恕我直言,有足够的理由放弃这一想法。

  2. 正如评论指出:即使在前台开始,服务可能会被破坏反正。只要保持这一点。然而,在我的经验,这种情况很少,而几乎从未 - 除非...

  3. ...这是随pre-安装启动管理器或能量储户(很多华为的设备,例如)设备。如果屏幕关闭后您的应用程序未配置适当的服务将被杀死几秒钟/分钟 - 或者它不会自动重新启动后,启动

  1. First of all: if you have two services in foreground, you have to have two notifications as well. This is considered as a security feature (check for instance this post). Having two icons in the status bar for one app might be quite confusing to the user and is IMHO reason enough to drop this idea.
  2. As stated in the comments: even if starting in foreground, a service might be destroyed anyway. Just keep this in mind. However, in my experience this happens rather rarely, almost never - except with ...
  3. ... devices that are shipped with pre-installed start managers or energy savers (many Huawei devices for instance). If your app is not configured appropriately your services will be killed several seconds/minutes after the screen turns off - or it will not automatically be started after reboot.

所以,一如既往这取决于你确切想达到什么;),但对我来说,它看起来像,如果你可以简单地在一个粘滞服务执行两个线程 - 我不能任何在这里看到缺点。

So, as always it depends on what exact you want to achieve ;) But to me it looks like as if you could simply implement two threads in one sticky service - I can't any see disadvantages here.

如果您的应用程序被通过任何类型的启动管理器崩溃或强制关闭的灭活,您的用户可能需要重新启动应用程序(请参阅this帖子包括应用程序状态)。

If your app gets inactivated by any kind of start manager, crash, or force close, your users might have to start the app again (see this post including app states).

希望这有助于!

这篇关于在前台启动两个服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆