Two methods of starting service in Android
2012-10-26 10:19
393 查看
Context.
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
Using startService() overrides the default service lifetime that is managed by
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
Parameters
Returns
If the service is being started or is already running, the
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
Note: this method can not be called from a
Parameters
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.
public abstract ComponentName startService (Intent service)
Since: API Level 1Request 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
SecurityExceptionif 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 anIntentFilterpublished by a service. Additional values may be included in the Intent extras to supply arguments along with this specific start call. |
---|
If the service is being started or is already running, the
ComponentNameof 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 1Connect 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
SecurityExceptionif you do not have permission to bind to the given service.
Note: this method can not be called from a
BroadcastReceivercomponent. 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
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_CREATE, BIND_DEBUG_UNBIND, BIND_NOT_FOREGROUND, BIND_ABOVE_CLIENT, BIND_ALLOW_OOM_MANAGEMENT, or BIND_WAIVE_PRIORITY. |
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.
相关文章推荐
- Two Methods of Multitexture In Cg
- While starting out of two DBs one DB getting error: ORA-01102: cannot mount database in EXCLUSIVE mo
- Android Studio出现 No service of type Factory available in ProjectScopeServices.的错误解决方案
- Android Studio出现Error:No service of type Factory available in ProjectScopeServices.
- Two Methods of Multitexture In Cg
- Android Studio出现Error:No service of type Factory available in ProjectScopeServices.
- Offical explanation of service in android
- Android Studio出现Error:No service of type Factory available in ProjectScopeServices.
- Android Studio出现Error:No service of type Factory available in ProjectScopeServices.
- Android Studio出现 No service of type Factory available in ProjectScopeServices. Error解决方案
- Obscured two methods of ScheduledExecutorService
- Android Studio出现Error:No service of type Factory available in ProjectScopeServices.
- Android Studio出现 No service of type Factory available in ProjectScopeServices. Error解决方案
- Android studio编译出现no service of type factory available in projectscopeservices
- Example of Using Google Maps Service and GPS in Android
- android in practice_Using a Service for caching data(portfolio project)
- No service of type Factory<LoggingManagerInternal> available in ProjectScopeServices.
- getSystemService() in Android
- (OK) running two Android-x86 in VirtualBox, they connect to "ethernet bridge"
- No service of type Factory<LoggingManagerInternal> available in ProjectScopeServices.