can you give me a simple example of an application with background service which uses bind/unbind methods to start and stop it? I was googling for it for a half-hour, but those examples use startService/stopService methods or are very difficult for me. thank you.
Bounded services are bounded to an activity which binds it and will work only till bounded activity is alive. while a unbounded service will work till the completion even after activity is destroyed.
A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC).
Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls. This interface describes the abstract protocol for interacting with a remotable object.
Stopping a service. You stop a service via the stopService() method. No matter how frequently you called the startService(intent) method, one call to the stopService() method stops the service. A service can terminate itself by calling the stopSelf() method.
You can try using this code:
protected ServiceConnection mServerConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { Log.d(LOG_TAG, "onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(LOG_TAG, "onServiceDisconnected"); } } public void start() { // mContext is defined upper in code, I think it is not necessary to explain what is it mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE); mContext.startService(intent); } public void stop() { mContext.stopService(new Intent(mContext, ServiceRemote.class)); mContext.unbindService(mServerConn); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With