Android开发使用Messenger及Handler进行通信的方法示例
本文实例讲述了Android开发使用Messenger及Handler进行通信的方法。分享给大家供大家参考,具体如下:
1.客户端service
publicclassMessageServiceextendsService{ privatestaticfinalintMSG_SET_VALUE=0x110; privateHandlermHandler=newHandler(){ publicvoidhandleMessage(MessagemsgFromClient){ //返回给客户的信息 MessagemsgToClient=Message.obtain(msgFromClient); switch(msgFromClient.what){ //msg客户端传来的消息 caseMSG_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(RemoteExceptione){ e.printStackTrace(); } break; default: break; } } }; privateMessengermMessager=newMessenger(mHandler); privateMessengercMessenger; @Override publicIBinderonBind(Intentintent){ returnmMessager.getBinder(); } @Override publicvoidonDestroy(){ cMessenger=null; super.onDestroy(); } }
2.服务器端service
publicclassMessageServiceextendsService{ privatestaticfinalintMSG_SET_VALUE=0x110; privateHandlermHandler=newHandler(){ publicvoidhandleMessage(MessagemsgFromClient){ //返回给客户的信息 MessagemsgToClient=Message.obtain(msgFromClient); switch(msgFromClient.what){ //msg客户端传来的消息 caseMSG_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(RemoteExceptione){ e.printStackTrace(); } break; default: break; } } }; privateMessengermMessager=newMessenger(mHandler); privateMessengercMessenger; @Override publicIBinderonBind(Intentintent){ returnmMessager.getBinder(); } @Override publicvoidonDestroy(){ cMessenger=null; super.onDestroy(); } }
3.客户端使用
publicclassMainActivityextendsActivity{ privatestaticfinalintMSG_SET_VALUE=0x110; privateTextViewmTVStatus;//连接状态 privateLinearLayoutmLLContainer;//用来添加接收到服务器返回的信息 privateMessengermService;//客户端信使 privatebooleanisConnecting;//是否连接 privateHandlermHandler=newHandler(){ publicvoidhandleMessage(MessagemsgFromServer){ switch(msgFromServer.what){ caseMSG_SET_VALUE: TextViewtv=(TextView)findViewById(msgFromServer.arg1); tv.setText(tv.getText()+"==>"+msgFromServer.arg2); break; } super.handleMessage(msgFromServer); }; }; privateMessengermMessenger=newMessenger(mHandler); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTVStatus=(TextView)findViewById(R.id.tv_status); mLLContainer=(LinearLayout)findViewById(R.id.ll_container); } inti=0; publicvoidsend(Viewview){ try{ inta=i++; intb=(int)(Math.random()*100); //创建一个tv,添加到LinearLayout中 TextViewtv=newTextView(MainActivity.this); tv.setText(a+"+"+b+"=caculating..."); tv.setId(a); mLLContainer.addView(tv); MessagemsgFromClient=Message.obtain(null,MSG_SET_VALUE,a,b); msgFromClient.replyTo=mMessenger; if(isConnecting){ //往服务端发送消息 mService.send(msgFromClient); } }catch(RemoteExceptione){ e.printStackTrace(); } } publicvoidbind(Viewview){ Intentservice=newIntent(); service.setAction("android.intent.action.MESSAGE_SERVER"); bindService(service,conn,Context.BIND_AUTO_CREATE); } privateServiceConnectionconn=newServiceConnection(){ @Override publicvoidonServiceDisconnected(ComponentNamename){ mService=null; isConnecting=false; mTVStatus.setText("已断开连接!"); } @Override publicvoidonServiceConnected(ComponentNamename,IBinderservice){ mService=newMessenger(service); isConnecting=true; mTVStatus.setText("连接成功!"); } }; publicvoidunbind(Viewview){ unbindService(conn); conn=null; } @Override protectedvoidonDestroy(){ unbindService(conn); conn=null; super.onDestroy(); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android通信方式总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。