- package com.xwangly.apidemo.app;
- import java.util.Random;
- 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.os.RemoteException;
- import android.util.Log;
- public class MessengerService extends Service {
- private String TAG = "MessengerService";
- @Override
- public void onDestroy() {
- // TODO Auto-generated method stub
- Log.i(TAG, "onDestroy");
- cMessenger = null;
- super.onDestroy();
- }
- @Override
- public boolean onUnbind(Intent intent) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onUnbind");
- return super.onUnbind(intent);
- }
- static final int MSG_REGISTER_CLIENT = 1;
- static final int MSG_UNREGISTER_CLIENT = 2;
- static final int MSG_SET_VALUE = 3;
- private Random random = new Random();
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- Log.i(TAG, "handleMessage");
- switch (msg.what) {
- case MSG_SET_VALUE:
- try {
- Message message = Message.obtain(null,
- MessengerService.MSG_SET_VALUE);
- message.arg1 = random.nextInt(100);
- //得到客户端的信使对象,并向它发送消息
- cMessenger = msg.replyTo;
- cMessenger.send(message);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- break;
- default:
- super.handleMessage(msg);
- }
- }
- };
- /**
- * 自己的信使对象
- */
- private Messenger mMessenger = new Messenger(mHandler);
- /**
- * 客户的信使
- */
- private Messenger cMessenger;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onBind");
- //返回自己信使的bindler,以供客户端通过这个bindler得到服务端的信使对象(通过new Messenger(bindler))
- return mMessenger.getBinder();
- }
- @Override
- public void onRebind(Intent intent) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onRebind");
- }
- }
MessengerServiceActivities.java
Java代码
-
- package com.xwangly.apidemo.app;
- import com.xwangly.apidemo.R;
- import android.app.Activity;
- import android.content.ComponentName;
- 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.util.Log;
- import android.view.View;
- import android.widget.TextView;
- public class MessengerServiceActivities {
- public static class Binding extends Activity implements
- View.OnClickListener {
- private String TAG = "Binding";
- TextView mCallbackText;
- private boolean isBound;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.messenger_service_binding);
- findViewById(R.id.bind).setOnClickListener(this);
- findViewById(R.id.unbind).setOnClickListener(this);
- mCallbackText = (TextView) findViewById(R.id.callback);
- mCallbackText.setText("Not attached.");
- }
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- Log.i(TAG, "handleMessage");
- switch (msg.what) {
- case MessengerService.MSG_SET_VALUE:
- mCallbackText.setText("Received from service: " + msg.arg1);
- break;
- default:
- super.handleMessage(msg);
- }
- }
- };
- /**
- * 自己的信使
- */
- private Messenger mMessenger;
- /**
- * 远程服务的信使
- */
- private Messenger rMessenger;
- private ServiceConnection connection = new ServiceConnection() {
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onServiceConnected");
- rMessenger = new Messenger(service);
- mMessenger = new Messenger(mHandler);
- sendMessage();
- }
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- rMessenger = null;
- }
- };
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(
- "com.xwangly.apidemo.app.messenger_service");
- switch (v.getId()) {
- case R.id.bind:
- if (!isBound) {
- isBound = bindService(intent, connection, BIND_AUTO_CREATE);
- //isBound = true;
- }else {
- sendMessage();
- }
- break;
- case R.id.unbind:
- if (isBound) {
- unbindService(connection);
- isBound = false;
- }
- break;
- default:
- break;
- }
- }
- /**
- * 使用服务端的信使向它发送一个消息。
- */
- private void sendMessage() {
- // TODO Auto-generated method stub
- Message message = Message.obtain(null, MessengerService.MSG_SET_VALUE);
- message.replyTo = mMessenger;
- try {
- rMessenger.send(message);
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }