温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android开发使用Messenger及Handler进行通信的方法示例

发布时间:2020-09-13 10:23:38 来源:脚本之家 阅读:301 作者:LovooGod 栏目:移动开发

本文实例讲述了Android开发使用Messenger及Handler进行通信的方法。分享给大家供大家参考,具体如下:

1. 客户端service

public class MessageService extends Service {
 private static final int MSG_SET_VALUE = 0x110;
 private Handler mHandler = new Handler() {
  public void handleMessage(Message msgFromClient) {
   // 返回给客户的信息
   Message msgToClient = Message.obtain(msgFromClient);
   switch (msgFromClient.what) {
   // msg 客户端传来的消息
   case MSG_SET_VALUE:
    try {
     msgToClient.what = MSG_SET_VALUE;
     SystemClock.sleep(2000);
     msgToClient.arg2 = msgFromClient.arg1 + msgFromClient.arg2;
     //msgFromClient.replyTo.send(msgToClient);
     //得到客户端的信使对象,并向它发送消息
     cMessenger = msgFromClient.replyTo;
     cMessenger.send(msgToClient);
    } catch (RemoteException e) {
     e.printStackTrace();
    }
    break;
   default:
    break;
   }
  }
 };
 private Messenger mMessager = new Messenger(mHandler);
 private Messenger cMessenger;
 @Override
 public IBinder onBind(Intent intent) {
  return mMessager.getBinder();
 }
 @Override
 public void onDestroy() {
  cMessenger = null;
  super.onDestroy();
 }
}

2. 服务器端service

public class MessageService extends Service {
 private static final int MSG_SET_VALUE = 0x110;
 private Handler mHandler = new Handler() {
  public void handleMessage(Message msgFromClient) {
   // 返回给客户的信息
   Message msgToClient = Message.obtain(msgFromClient);
   switch (msgFromClient.what) {
   // msg 客户端传来的消息
   case MSG_SET_VALUE:
    try {
     msgToClient.what = MSG_SET_VALUE;
     SystemClock.sleep(2000);
     msgToClient.arg2 = msgFromClient.arg1 + msgFromClient.arg2;
     //msgFromClient.replyTo.send(msgToClient);
     //得到客户端的信使对象,并向它发送消息
     cMessenger = msgFromClient.replyTo;
     cMessenger.send(msgToClient);
    } catch (RemoteException e) {
     e.printStackTrace();
    }
    break;
   default:
    break;
   }
  }
 };
 private Messenger mMessager = new Messenger(mHandler);
 private Messenger cMessenger;
 @Override
 public IBinder onBind(Intent intent) {
  return mMessager.getBinder();
 }
 @Override
 public void onDestroy() {
  cMessenger = null;
  super.onDestroy();
 }
}

3. 客户端使用

public class MainActivity extends Activity {
 private static final int MSG_SET_VALUE = 0x110;
 private TextView mTVStatus; // 连接状态
 private LinearLayout mLLContainer;// 用来添加接收到服务器返回的信息
 private Messenger mService; // 客户端信使
 private boolean isConnecting; // 是否连接
 private Handler mHandler = new Handler() {
  public void handleMessage(Message msgFromServer) {
   switch (msgFromServer.what) {
   case MSG_SET_VALUE:
    TextView tv = (TextView) findViewById(msgFromServer.arg1);
    tv.setText(tv.getText() + "==>" + msgFromServer.arg2);
    break;
   }
   super.handleMessage(msgFromServer);
  };
 };
 private Messenger mMessenger = new Messenger(mHandler);
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mTVStatus = (TextView) findViewById(R.id.tv_status);
  mLLContainer = (LinearLayout) findViewById(R.id.ll_container);
 }
 int i = 0;
 public void send(View view){
  try {
   int a = i++;
   int b = (int) (Math.random() * 100);
   //创建一个tv,添加到LinearLayout中
   TextView tv = new TextView(MainActivity.this);
   tv.setText(a + " + " + b + " = caculating ...");
   tv.setId(a);
   mLLContainer.addView(tv);
   Message msgFromClient = Message.obtain(null, MSG_SET_VALUE, a, b);
   msgFromClient.replyTo = mMessenger;
   if (isConnecting) {
    //往服务端发送消息
    mService.send(msgFromClient);
   }
  } catch (RemoteException e) {
   e.printStackTrace();
  }
 }
 public void bind(View view) {
  Intent service = new Intent();
  service.setAction("android.intent.action.MESSAGE_SERVER");
  bindService(service, conn, Context.BIND_AUTO_CREATE);
 }
 private ServiceConnection conn = new ServiceConnection() {
  @Override
  public void onServiceDisconnected(ComponentName name) {
   mService = null;
   isConnecting = false;
   mTVStatus.setText("已断开连接!");
  }
  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
   mService = new Messenger(service);
   isConnecting = true;
   mTVStatus.setText("连接成功!");
  }
 };
 public void unbind(View view) {
  unbindService(conn);
  conn = null;
 }
 @Override
 protected void onDestroy() {
  unbindService(conn);
  conn = null;
  super.onDestroy();
 }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android通信方式总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI