您的位置:首页 > 移动开发 > Android开发

Two methods of starting service in Android

2012-10-26 10:19 393 查看
Context.

public abstract ComponentName startService (Intent service)

Since: API Level 1

Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

Every call to this method will result in a corresponding call to the target service's
onStartCommand(Intent, int, int)
method
, with the intent given here. This provides a convenient way to submit jobs to a service without having to bind and call on to its interface.

Using startService() overrides the default service lifetime that is managed by
bindService(Intent, ServiceConnection, int)
: it requires the service to remain running until
stopService(Intent)
is called, regardless of whether any clients are connected to it.
Note that calls to startService() are not nesting: no matter how many times you call startService(), a single call to
stopService(Intent)
will stop it.

The system attempts to keep running services around as much as possible. The only time they should be stopped is if the current foreground application is using so many resources that the service needs to be killed. If any errors happen in the service's process, it will automatically be restarted.

This function will throw
SecurityException
if you do not have permission to start the given service.

Parameters
serviceIdentifies the service to be started. The Intent may specify either an explicit component name to start, or a logical description (action, category, etc) to match an
IntentFilter
published by a service. Additional values may be included in the Intent extras to supply arguments along with this specific start call.
Returns

If the service is being started or is already running, the
ComponentName
of the actual service that was started is returned; else if the service does not exist null is returned.

public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)

Since: API Level 1

Connect to an application service, creating it if needed. This defines a dependency between your application and the service. The given conn will receive the service object when it is created and be told if it dies and restarts. The service will be considered required by the system only for as long as the calling context exists. For example, if this Context is an Activity that is stopped, the service will not be required to continue running until the Activity is resumed.

This function will throw
SecurityException
if you do not have permission to bind to the given service.

Note: this method can not be called from a
BroadcastReceiver
component. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call
startService(Intent)
with the arguments containing the command to be sent, with the service calling its
stopSelf(int)
method when done executing that command. See the API demo App/Service/Service Start Arguments Controller for an illustration of this. It is okay, however, to use this method from a BroadcastReceiver that has been registered with
registerReceiver(BroadcastReceiver, IntentFilter)
, since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).

Parameters
serviceIdentifies the service to connect to. The Intent may specify either an explicit component name, or a logical description (action, category, etc) to match an
IntentFilter
published by a service.
connReceives information as the service is started and stopped.
flagsOperation options for the binding. May be 0,
BIND_AUTO_CREATE
,
BIND_DEBUG_UNBIND
,
BIND_NOT_FOREGROUND
,
BIND_ABOVE_CLIENT
,
BIND_ALLOW_OOM_MANAGEMENT
, or
BIND_WAIVE_PRIORITY
.
Returns

If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐