Android Bind Service with Messenger
Hi All,
This example shows how we can bind a service and communicate via Handler.
Following is the Activity,
package com.aidl.messenger; 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.Handler; import android.os.IBinder; import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.widget.Toast; public class AIDLMessengerActivity extends Activity { /** Called when the activity is first created. */ private Messenger messenger; private MyServiceConnection conn; final Messenger mMessenger = new Messenger(new IncomingHandler()); class IncomingHandler extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch(msg.what){ case 2: Bundle bundle = msg.getData(); Toast.makeText(AIDLMessengerActivity.this, bundle.getString("reply"), Toast.LENGTH_SHORT).show(); break; } } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // intent.setClassName("com.aidl.messenger", // "com.aidl.messenger.MyService"); conn = new MyServiceConnection(); boolean mIsBound = bindService(new Intent(this, MyService.class), conn, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { unbindService(conn); super.onDestroy(); } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName arg0, IBinder binder) { messenger = new Messenger(binder); Message msg = Message.obtain(null, MyService.MESSAGE_TEST, 0, 0); Bundle bundle = new Bundle(); bundle.putString("name", "Jihad"); msg.setData(bundle); msg.replyTo = mMessenger; try { messenger.send(msg); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName arg0) { messenger = null; } } }
package com.aidl.messenger; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.widget.Toast; public class MyService extends Service { public static final int MESSAGE_TEST = 1; public static final int MESSAGE_TEST_REPLY = 2; private Messenger mMessenger = null; private String mesg = ""; class MyHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MESSAGE_TEST: mesg = "Hi "; Toast.makeText(MyService.this, "received in service", Toast.LENGTH_SHORT).show(); mMessenger = msg.replyTo; try { Bundle bundle = msg.getData(); String name = bundle.getString("name"); Message newMsg = new Message(); newMsg.what = MESSAGE_TEST_REPLY; bundle.clear(); bundle.putString("reply", mesg +name+", Welcome to Service"); newMsg.setData(bundle); mMessenger.send(newMsg); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } } public void testMethod(){ Toast.makeText(MyService.this, "Test method", Toast.LENGTH_SHORT).show(); } private Messenger messenger = new Messenger(new MyHandler()); @Override public IBinder onBind(Intent intent) { return messenger.getBinder(); } }
and Manifest,
In Activity, We have one Messenger associated with a Handler. This is for handling the message from the service.
In Service, the Messenger will handle the request from the activity.
Thanks,
wow its too good! very usefull... thanks for sharing
ReplyDeletethanks peter.. Keep reading.. I will keep posting new topics..
DeleteGreat Work Jihad . Hope U keep up this good works
ReplyDeleteJihad , I Think you need to add Little more Desc On what your are going to do rather than putting down entire code . It is Awesome Work !!! . We all are proud of you
ReplyDelete@sujith.. thanks . it inspired me lot to focus on sharing ideas. :)
ReplyDeleteHello,
DeleteI am working on app where i have to show icon on window from service.
i am creating object of MainActiviy in onCreate() of service.
and inside handler doing like
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
image.setImageResource(R.mipmap.ic_launcher);
}
});
but when app is minimized or removed from memory service is running but problem is getting null
mainactiviy=null.
so icon is not changed.
Please help.
Thanks in advance
really great work jihad thanks for sharing most interesting topic
ReplyDeleteGood work man very useful contents..........Next time please try to add some descriptions also
ReplyDeletevery good example.it helped me a lot.
ReplyDeleteThanks
Thanks everyone.
ReplyDeleteafter 2 days of trying to find examples, I can confidently say this explains it in the best and cleanest way, thanks! :)
ReplyDelete