CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Android Programming – Create Bounded Android Service

In our previous Articles; initially, we have developed a Service and later we have converted the Service to an Android Service. And in our previous article, we have discussed that we can develop bounded and unbounded Services in Android. We already developed an unbounded Service in our previous Article “Android Programming – Develop an Android Service“.

In this article, we will discuss developing a bounded Service.

Create Bounded Android Service

If you see the Android Service life cycle, for bounded Services; there are onBind, onUnbind and onRebind functions need to implement as part of the Service life cycle.

What we need to do now is, add these call-back functions into our existing Android Service (unbounded service) what we have created in our previous Article “Android Programming – Develop an Android Service“.

Step 1. We are going to create a bounded Android service. Android provides different ways to create bounded Android services. We use the simple one, by extending the Binder class.

The purpose of our Binder class is to provide a method to return an instance to our Service class to the component that activates the Service. In this case, it is the Activity that is going to activate the Service.

Below is the code snippet that must be added to our Service class. Please note that we are adding this class local to our Service class. And the Service must be Run within the same process where the component (in this case, Activity) is Running.

public class MyBinder extends Binder {
    MyService getService() {
        return MyService.this;
    }
}

Step 2. Now we need to implement Service life cycle methods as mentioned above.

onBind function (actually it is a callback function) is required to provide the instance to the Binder class; from where the component gets the Service instance to call the methods defined in the Service. In our example, it is the Activity that is going to activate the Service. This function should be defined in our Service class. In short, onBind should return the instance of Binder class. Note that, in our unbounded Service; onBind returns a null value.

Similar to onBind, we have to implement onUnbind and onRebind functions. Nothing much about these functions to discuss for this example. See the code below complete example.

MyService class code

Here is the complete code of MyService class.

Step 3. Now we need to add the code to activate the Service. For unbounded Service, we have used startService and stopService methods to activate and stop the Service.

As this is a bounded Service, we have to call bindService to bind to the Service and once the job is done, we have to call unbindService to stop the Service.

Let’s discuss more these functions.

Step 3.1. The Syntax of bindService function is:

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

Where service is the instance of an Intent to an Android Service.

And conn is the instance of ServiceConnection class. This connection class receives the Service object when the Service is created. And also receives information when the Service is restarted or destroyed. This must not be the null value.

The last argument flags contains the options for the binding. Let’s keep this value BIND_AUTO_CREATE for the time being. This tells, automatically create the Service. We may discuss more on this in another article.

Step 3.2. The Syntax of unbindService function is:

public abstract void unbindService (ServiceConnection conn)

Where conn is the already created ServiceConnection object.

unbindService disconnect the Service to the component (or Activity).

Step 4. We will add the code in the button handler:

  • Create the Service and once the Service is available,
  • Call its members and once the job is done,
  • Destroy the Service.

Activity class code

The complete code of the Activity class is added here.

Step 5. Build and Run the Project. You will see a “HELLO!” button in the Emulator. Click on it, to see the Magic!

We will discuss more in my upcoming Articles.

[..] David

Android Programming – Create Bounded Android Service

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top