Search This Blog

Wednesday, 20 February 2013

Binding service using Messenger in android

Binding service using Messanger is another way. Here we discussed bounding service using iBinder. This current post address the issue of bounding service using Messanger
When to use Messanger Approach
If you need your service to communicate with remote processes, then you can use a Messenger to provide the interface for your service. This technique allows you to perform interprocess communication (IPC) without the need to use AIDL.

Create one Class and extend Service. Register this inside android manifest.
Not make necessary changes to your service class. Create one handler class for Messanger. Handler class will handle communication with Activity.

 import android.app.Service;  
 import android.content.Intent;  
 import android.os.Handler;  
 import android.os.IBinder;  
 import android.os.Message;  
 import android.os.Messenger;  
 import android.widget.Toast;  
 public class UsingMessanger extends Service {  
 final public static int Msg = 1;  
 @Override  
 public void onCreate() {  
 super.onCreate();  
 }  
 @Override  
 public IBinder onBind(Intent arg0) {  
 Messenger mssMessenger = new Messenger(new HandlerMessag());  
 return mssMessenger.getBinder();  
 }  
 public class HandlerMessag extends Handler {  
 @Override  
 public void handleMessage(Message msg) {  
 switch (msg.what) {  
 case Msg:  
 Toast.makeText(getApplicationContext(), "Service: I got your message",  
 Toast.LENGTH_SHORT).show();  
 break;  
 default:  
 super.handleMessage(msg);  
 }  
 }  
 }  
 }  
Now change the activity to handle Service connection.
 import android.app.Activity;  
 import android.content.ComponentName;  
 import android.content.Context;  
 import android.content.Intent;  
 import android.content.ServiceConnection;  
 import android.os.Bundle;  
 import android.os.IBinder;  
 import android.os.Message;  
 import android.os.Messenger;  
 import android.os.RemoteException;  
 public class MessangerActivity extends Activity {  
 Messenger messenger;  
 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 bindService();  
 }  
 public void bindService() {  
 Intent intent = new Intent(this, UsingMessanger.class);  
 bindService(intent, new ConnectService(), Context.BIND_AUTO_CREATE);  
 }  
 public void stoppingService() {  
 Intent intent = new Intent(this, UsingMessanger.class);  
 stopService(intent);  
 }  
 public class ConnectService implements ServiceConnection {  
 @Override  
 public void onServiceConnected(ComponentName name, IBinder service) {  
 messenger = new Messenger(service);  
 Message msg = Message.obtain(null, UsingMessanger.Msg, 0, 0);  
 try {  
 messenger.send(msg);  
 } catch (RemoteException e) {  
 e.printStackTrace();  
 }  
 }  
 @Override  
 public void onServiceDisconnected(ComponentName name) {  
 /**  
 * Called when service disconnected  
 */  
 }  
 }  
 }  


 All Service Binding Source Code

Binding service using iBinder and updating UI in android

Binding service is used when you want to communicate with service either it for updating UI with the work you are doing in service or using values of service's current instance. See binding service using Messanger

You can bind service by multiple ways but here i explain using iBinder method.

Some Fact
You can only bind service with activity not with the BroadcastReceiver

Binding Purpose
To get current instance of Service to interact with its member
When to use iBinder Approach
If your service is private to your own application and runs in the same process as the client (which is common), you should create your interface by extending the Binder class and returning an instance of it from onBind()
Binding service automatically start service so use either bindService() or both methods. UsingiBinder is service class

public void startingService() {
Intent intent = new Intent(this, UsingiBinder.class);
startService(intent);
}

public void bindService() {
Intent intent = new Intent(this, UsingiBinder.class);
bindService(intent, new ConnectService(), 0);
}

ConnectService is ServiceConnection that provide you bridge between Service and activity. It has two method

onServiceConnected(ComponentName name, IBinder service)
onServiceDisconnected(ComponentName name)

Service class has method onBind(Intent arg0) that help to return service instance in the form of iBinder

 import android.app.Service;  
 import android.content.Intent;  
 import android.os.Binder;  
 import android.os.IBinder;  
 public class UsingiBinder extends Service {  
 @Override  
 public IBinder onBind(Intent arg0) {  
 return iBinder;  
 }  
 private IBinder iBinder = new ContainsLocal();  
 public class ContainsLocal extends Binder {  
 UsingiBinder getBinder() {  
 return UsingiBinder.this;  
 }  
 }  
 }  

Download All Service Binding Example

Android News and source code