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 untilstopService(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
service Identifies 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 anIntentFilter 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 callstartService(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 withregisterReceiver(BroadcastReceiver, IntentFilter), since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).

 

Parameters
service Identifies the service to connect to. The Intent may specify either an explicit component name, or a logical description (action, category, etc) to match an IntentFilterpublished by a service.
conn Receives information as the service is started and stopped.
flags Operation options for the binding. May be 0, BIND_AUTO_CREATEBIND_DEBUG_UNBINDBIND_NOT_FOREGROUNDBIND_ABOVE_CLIENTBIND_ALLOW_OOM_MANAGEMENT, orBIND_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.
posted on 2012-10-26 10:19  Oceanedge  阅读(189)  评论(0)    收藏  举报