它是如何可能的服务无限期地运行,也允许在Android的结合? [英] How it is possible Service run indefinitely and also allow binding in android?

查看:98
本文介绍了它是如何可能的服务无限期地运行,也允许在Android的结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想它可以在后台运行,直到我停下来,即使启动它的成分被破坏一个服务,也可以结合的活动。它是如何可能的?

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 ?

根据Android的服务绑定文件 - 有三种方式建立绑定服务

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

  1. 在扩展粘合剂类。
  2. 使用信使。
  3. 使用AIDL。

我已经创建了使用信使(第二法)绑定服务。活动是结合其ONSTART()方法来提供服务,并解除其的onStop()方法。双向消息(活动和服务之间)工作正常。但问题是,当活动解除绑定的服务,服务被破坏。但我想它可以无限期地运行服务。

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.

有可能成为Android 服务开发指南 - 虽然此文件一般讨论这两个服务类型分开,你的服务可以是双向的,它可以启动(无限期运行),并允许绑定它只是你是否实现了几个回调方法的问题:onStartCommand(),以允许组件启动和onBind (),以允许结合。

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."

我也实现服务onStartCommand()方法,并返回START_STICKY,但它永远不会被调用。纵观界服务开发指南的生命周期回调,没有onStartCommand()回调方法。那么它是如何可能的运行服务,直到我们停下来,还允许绑定?

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?

我使用的Eclipse平台的Fedora 15操作系统。

I am using eclipse platform in fedora 15 OS.

任何帮助......

Any Help.....

推荐答案

您只需要使用()地方启动它startService。这将prevent它被自动停止时,有没有更多的绑定。

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

服务文档,重点煤矿:

一个服务可以同时起步,并没有绑定到它的连接。在   这样的情况下,系统将保持服务运行只要   无论是启动还是有一个或多个连接到它与   该Context.BIND_AUTO_CREATE标志。

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 it with the Context.BIND_AUTO_CREATE flag.

正如其他人所指出的那样,它仍然可以通过机器人杀死,如果需要的资源。你可以在优先的服务,并使其不太可能,如果你让一个​​前台服务被打死

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的结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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