Wednesday, 8 April 2015

Bind Service to Activity in Android.

Binding Activities to Services

So far, you have seen how services are created and how they are called and terminated when they are done with their task. 
All the services that you have seen are simple — either they start with a counter and increment at regular intervals, or they download a fixed set of files from the Internet. 
However, real-world services are usually much more sophisticated, requiring the passing of data so that they can do the job correctly for you. 
Using the service demonstrated earlier that downloads a set of files, suppose you now want to let the calling activity determine what files to download, instead of hardcoding them in the service. 
Here is what you need to do. First, in the calling activity, you create an Intent object, specifying the service name:

​​​​​​​​Button ​btnStart​=​(Button)​findViewById(; 
​​​​​​​​​​​​public ​void ​onClick(View ​v)​
​​​​​​​​​​​​​​​​Intent intent = new Intent(getBaseContext(), MyService.class); ​
You then create an array of URL objects and assign it to the Intent object through its putExtra() method. 
Finally, you start the service using the Intent object:
​​​​​​​​Button ​btnStart​=​(Button)​findViewById(; 
​​​​​​​​btnStart.setOnClickListener(new​ View.OnClickListener()
​​​​​​​​​​​​public ​void ​onClick(View ​v)
​​​​​​​​​​​​​​​​Inten t​intent​=​new​Intent(getBaseContext(),​MyService.class); ​
​​​​​​​​​​​​​​​​​​​​URL[] urls = new URL[] 
​new URL(“”), ​
​​​​​​​​​​​​​​​​​​​​​​​​​​​new URL(“”), 
​​​​​​​​​​​​​​​​​​​​​​​​​​​​new URL(“”), 
​​​​​​​​​​​​​​​​​​​​​​​​​​​​new URL(“”)}; 

​​​​​​​​​​​​​​​​​​​​intent.putExtra(“URLs”, urls); 
catch (MalformedURLException e) 
Note that the URL array is assigned to the Intent object as an Object array. 
On the service’s end, you need to extract the data passed in through the Intent object in the onStartCommand() method:
​​​​@Override ​​​
​public​ int ​onStartCommand(Intent​ intent,​int ​flags,​int ​startId)​
​​​​​​​​//​We ​want​ this ​service​ to​ continue ​running​ until ​it ​is ​explicitly 
​​​​​​​​//​stopped,​so ​return ​sticky. ​

Object [] objUrls = (Object []) intent.getExtras().get(“URLs”); ​​​​​​
​​URL [] urls = new URL[objUrls.length]; ​​

​​​​​​for (int i=0; i<objUrls.length-1; i++) 
​​​​​​​​​​​​urls [i] = (URL) objUrls[i]; 
​​​​​​​​new DoBackgroundTask().execute(urls); 
​​​​​​​​return ​START_STICKY; ​​​​
The preceding first extracts the data using the getExtras() method to return a Bundle object. 
It then uses the get() method to extract out the URL array as an Object array. 
Because in Java you cannot directly cast an array from one type to another, you have to create a loop and cast each member of the array individually. 
Finally, you execute the background task by passing the URL array into the execute() method. 
This is one way in which your activity can pass values to the service. 
As you can see, if you have relatively complex data to pass to the service, you have to do some additional work to ensure that the data is passed correctly. 
A better way to pass data is to bind the activity directly to the service so that the activity can call any public members and methods on the service directly.


  1. hi,
    can you mail me the source code files pls

  2. Hello divyank,

    i think u have to try first in your way with the help of this post, if again stuck then sure i will help u.