java 服务如何无限期运行并允许在 android 中绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7292533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:26:20  来源:igfitidea点击:

How it is possible Service run indefinitely and also allow binding in android?

javaandroidandroid-service

提问by Khushbu Shah

I want a service which can run in the background until I stop, even if the component that started it is destroyed and also allows binding to the activities. How it is possible ?

我想要一个可以在后台运行直到我停止的服务,即使启动它的组件被破坏并且还允许绑定到活动。怎么可能?

As per android bound services document - there are three ways of creating bound service

根据android绑定服务文档 - 创建绑定服务的三种方式

  1. Extending Binder class.
  2. Using Messenger.
  3. Using AIDL.
  1. 扩展 Binder 类。
  2. 使用信使。
  3. 使用 AIDL。

I have created a bound service using messenger (2nd method). Activity is bind to service in its onStart() method and unbind in its onStop() method. Two way messaging (between activity and service) works properly. But problem is that when activity unbinds service, service is destroyed. But I want a service which can run indefinitely.

我使用 messenger(第二种方法)创建了一个绑定服务。活动在其 onStart() 方法中绑定到服务,并在其 onStop() 方法中解除绑定。两种方式的消息传递(在活动和服务之间)正常工作。但问题是当活动解除服务绑定时,服务被破坏。但我想要一个可以无限期运行的服务。

It is possible as android Services Dev Guide- "Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding."

可以作为 android服务开发指南- “虽然本文档通常分别讨论这两种类型的服务,但您的服务可以两种方式工作 - 它可以启动(无限期运行)并允许绑定。这只是您是否实现几个回调方法:onStartCommand() 允许组件启动它,onBind() 允许绑定。”

I also implement onStartCommand() method in service and return START_STICKY, but it is never called. Looking at the lifecycle callbacks of bounded service in dev guide, there is no onStartCommand() callback method. Then how it is possible to run service until we stop and also allow binding?

我还在服务中实现了 onStartCommand() 方法并返回 START_STICKY,但它从未被调用。在开发指南中查看有界服务的生命周期回调,没有 onStartCommand() 回调方法。那么如何在我们停止并允许绑定之前运行服务?

I am using eclipse platform in fedora 15 OS.

我在 Fedora 15 OS 中使用 eclipse 平台。

Any Help.....

任何帮助.....

回答by Joel F

You just need to start it with startService()somewhere. This will prevent it from being stopped automatically when there are no more bindings.

你只需startService()要从某个地方开始。这将防止它在没有更多绑定时自动停止。

From the Service documentation, emphasis mine:

服务文档中,强调我的:

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to itwith the Context.BIND_AUTO_CREATE flag.

一个服务既可以启动也可以绑定连接。在这种情况下,只要服务已启动,或者存在一个或多个带有 Context.BIND_AUTO_CREATE 标志的连接,系统就会保持服务运行 。

As others have pointed out, it could still be killed by Android if resources are needed. You can "prioritize" your Service and make it less likely to be killed if you make it a foreground service.

正如其他人指出的那样,如果需要资源,它仍然可能被 Android 杀死。您可以“优先化”您的服务,如果您将其设为前台服务,则可以降低其被杀死的可能性。

回答by NickT

I've not used services with the messenger service, but I have bound to a remote service with a remote (AIDL) interface. My findings may be of some help. As my main activity and service are currently implemented, I bind to the service like you do with code like

我没有将服务与信使服务一起使用,但我已绑定到具有远程 (AIDL) 接口的远程服务。我的发现可能会有所帮助。由于我的主要活动和服务目前已实现,因此我像使用代码一样绑定到服务

mServiceConnected = bindService(new Intent("com.mypackage.MyService.SERVICE"), this,
                Context.BIND_AUTO_CREATE);

My activity implements ServiceConnection

我的活动实现了 ServiceConnection

When I call unbindService(this)as the activity ends, then like you have found, the service's onDestroy() method is called.

当我在活动结束时调用unbindService(this) 时,就像您发现的那样,将调用服务的 onDestroy() 方法。

If however, prior to the bindService line, I also explicitly start the service with

但是,如果在 bindService 行之前,我还显式地启动服务

startService(new Intent("com.mypackage.MyService.SERVICE"));

then the unBind does not cause the service's onDestroy() to execute. It's still necessary call unbindService in the activity's onDestroy/Stop, otherwise you will leak a service connection.

那么 unBind 不会导致服务的 onDestroy() 执行。还是需要在activity的onDestroy/Stop中调用unbindService,否则会泄露服务连接。

In my case, presumably the service remains available for other applications to bind to via its remote interface.

在我的情况下,大概该服务仍然可供其他应用程序通过其远程接口绑定。

回答by Idolon

Service.onStartCommandcallback will be called only when you start your service using startServicemethod. As @NickT and @JoelF already pointed you need to call startService()besides the bindService()call somewhere in your client code (e.g. in onCreate).

Service.onStartCommand只有当您使用startService方法启动服务时才会调用回调。正如@NickT 和@JoelF 已经指出的那样,startService()除了bindService()调用客户端代码中的某个地方(例如在 onCreate 中)之外,您还需要调用。

You might also want to have a look at this (a little bit old, but still useful) article: "Double life of a service" and try the example program author provided.

您可能还想看看这篇(有点旧,但仍然有用)文章:“服务的双重生命”并尝试作者提供的示例程序。

回答by shanraisshan

In order to perform communication between serviceand activity. You can also use Binder as mentioned in Official Android Example

为了在服务活动之间进行通信。您还可以使用官方 Android 示例中提到的 Binder

http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

http://developer.android.com/reference/android/app/Service.html#LocalServiceSample

As official android documents suggests http://developer.android.com/guide/components/services.html#StartingAService

由于官方android文档建议 http://developer.android.com/guide/components/services.html#StartingAService

Although this documentation generally discusses these two types of services separately, your service can work both ways—it can be started (to run indefinitely) and also allow binding. It's simply a matter of whether you implement a couple callback methods: onStartCommand() to allow components to start it and onBind() to allow binding.

尽管本文档通常分别讨论这两种类型的服务,但您的服务可以两种方式工作——它可以启动(无限期运行),也可以允许绑定。这只是您是否实现几个回调方法的问题:onStartCommand() 允许组件启动它,而 onBind() 允许绑定。

This project implement this mixture of service (BothService) and shows that how can a Servicerun indefinitely and also allow binding with multiple activities.

该项目实现了这种服务的混合 ( BothService),并展示了一个服务如何无限期地运行并允许绑定多个活动

https://github.com/shanrais/BothService

https://github.com/shanrais/BothService

回答by Abhijeet Pathak

If you add the "Ongoing Notification"in the Android App Drawer, then your app and service won't be killed.

如果您"Ongoing Notification"在 Android 应用程序抽屉中添加,那么您的应用程序和服务不会被杀死。

Check out http://developer.android.com/guide/topics/ui/notifiers/notifications.html

查看http://developer.android.com/guide/topics/ui/notifiers/notifications.html

回答by Qumber Abbas

You can use service binder and make a single instance of connection at app instance level, in onCreate of the App, this will keep service alive. The service must be foreground service

您可以使用服务绑定器并在应用程序实例级别创建单个连接实例,在应用程序的 onCreate 中,这将使服务保持活动状态。服务必须是前台服务