private FirstService mFirstService; private ServiceConnection mFirstConn = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder service) { mFirstService = ((FirstService.LocalBinder) service).getService(); } public void onServiceDisconnected(ComponentName name) { mFirstService = null; } };
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; public class MessageService extends Service { private static final String TAG = "MessageService"; private Messenger mRecvMsg; private Messenger mReplyMsg; private Bundle mBundle; Handler mServerHandler = new Handler(){ public void handleMessage(Message msg) { mReplyMsg = msg.replyTo; mBundle = msg.getData(); new ReplyThread().start(); } }; class ReplyThread extends Thread { public void run() { String desc = String.format("请求参数为%s,应答参数为%s", mBundle.getString("msg"), "bbb"); Bundle bundle = new Bundle(); bundle.putString("msg", desc); Message msg = Message.obtain(); msg.setData(bundle); try { mReplyMsg.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } } @Override public void onCreate() { mRecvMsg = new Messenger(mServerHandler); super.onCreate(); } @Override public IBinder onBind(Intent intent) { return mRecvMsg.getBinder(); } }
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.Message; import android.os.Messenger; import android.os.RemoteException; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MessengerActivity extends Activity implements OnClickListener { private static final String TAG = "MessengerActivity"; private TextView tv_process; private Messenger mSendMsg; private Messenger mReplyMsg; Handler mClientHandler = new Handler(){ public void handleMessage(Message msg) { Bundle bundle = msg.getData(); tv_process.setText(bundle.getString("msg")); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_messenger); tv_process = (TextView) findViewById(R.id.tv_process); Button btn_messenger_start = (Button) findViewById(R.id.btn_messenger_start); btn_messenger_start.setOnClickListener(this); mReplyMsg = new Messenger(mClientHandler); Intent intent = new Intent(this, MessageService.class); bindService(intent, mMessageConn, Context.BIND_AUTO_CREATE); } private ServiceConnection mMessageConn = new ServiceConnection() { public void onServiceConnected(ComponentName name, IBinder binder) { mSendMsg = new Messenger(binder); } public void onServiceDisconnected(ComponentName name) { } }; @Override public void onClick(View v) { if (v.getId() == R.id.btn_messenger_start) { Bundle bundle = new Bundle(); bundle.putString("msg", "aaa"); Message msg = Message.obtain(); msg.setData(bundle); msg.replyTo = mReplyMsg; try { mSendMsg.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } } }