Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bind/unbind service example (android)

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.

like image 528
user1049280 Avatar asked Dec 01 '11 12:12

user1049280


People also ask

What is bound and unbound service in Android?

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.

What is bound service in Android example?

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

What is Ibinder Android?

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.

How do you stop a service in Android?

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.


1 Answers

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); } 
like image 161
Dawid Sajdak Avatar answered Sep 29 '22 15:09

Dawid Sajdak