Android开发使用信使及处理程序进行通信的方法示例

  

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

  

        公开课MessageService延伸服务{   私有静态最终int MSG_SET_VALUE=https://www.yisu.com/zixun/0x110;   私人处理程序mHandler=new处理程序(){   公共空间handleMessage(消息msgFromClient) {//返回给客户的信息   消息msgToClient=Message.obtain (msgFromClient);   开关(msgFromClient.what) {//味精客户端传来的消息   案例MSG_SET_VALUE:   尝试{   msgToClient。什么=MSG_SET_VALUE;   SystemClock.sleep (2000);   msgToClient。最长=msgFromClient。__arg1 + msgFromClient.arg2;//msgFromClient.replyTo.send (msgToClient);//得到客户端的信使对象,并向它发送消息   cMessenger=msgFromClient.replyTo;   cMessenger.send (msgToClient);   }抓住RemoteException (e) {   e.printStackTrace ();   }   打破;   默认值:   打破;   }   }   };   私人信使mMessager=new信使(mHandler);   私人信使cMessenger;   @Override   公共内部>   公开课MessageService延伸服务{   私有静态最终int MSG_SET_VALUE=https://www.yisu.com/zixun/0x110;   私人处理程序mHandler=new处理程序(){   公共空间handleMessage(消息msgFromClient) {//返回给客户的信息   消息msgToClient=Message.obtain (msgFromClient);   开关(msgFromClient.what) {//味精客户端传来的消息   案例MSG_SET_VALUE:   尝试{   msgToClient。什么=MSG_SET_VALUE;   SystemClock.sleep (2000);   msgToClient。最长=msgFromClient。__arg1 + msgFromClient.arg2;//msgFromClient.replyTo.send (msgToClient);//得到客户端的信使对象,并向它发送消息   cMessenger=msgFromClient.replyTo;   cMessenger.send (msgToClient);   }抓住RemoteException (e) {   e.printStackTrace ();   }   打破;   默认值:   打破;   }   }   };   私人信使mMessager=new信使(mHandler);   私人信使cMessenger;   @Override   公共内部>   公开课MainActivity延伸活动{   私有静态最终int MSG_SET_VALUE=https://www.yisu.com/zixun/0x110;   私人TextView mTVStatus;//连接状态   私人LinearLayout mLLContainer;//用来添加接收到服务器返回的信息   私人信使mService;//客户端信使   私人布尔isConnecting;//是否连接   私人处理程序mHandler=new处理程序(){   公共空间handleMessage(消息msgFromServer) {   开关(msgFromServer.what) {   案例MSG_SET_VALUE:   TextView电视=(TextView) findViewById (msgFromServer.arg1);   tv.setText (tv.getText() +”==比;“+ msgFromServer.arg2);   打破;   }   super.handleMessage (msgFromServer);   };   };   私人信使mMessenger=new信使(mHandler);   @Override   保护无效onCreate(包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;   公共空间发送(查看视图){   尝试{   int=我+ +;   int b=(int) (math . random () * 100);//创建一个电视,添加到LinearLayout中   TextView电视=new TextView (MainActivity.this);   电视。setText (a + " + " + + "=计算…”);   tv.setId(一个);   mLLContainer.addView(电视);   消息msgFromClient=消息。获得(null, MSG_SET_VALUE, a, b);   msgFromClient。replyTo=mMessenger;   如果(isConnecting) {//往服务端发送消息   mService.send (msgFromClient);   }   }抓住RemoteException (e) {   e.printStackTrace ();   }   }   公共空间绑定(查看视图){   意图服务=new意图();   service.setAction (“android.intent.action.MESSAGE_SERVER”);   bindService(服务、康涅狄格州、Context.BIND_AUTO_CREATE);   }   私人ServiceConnection康涅狄格州=new ServiceConnection () {   @Override   公共空间onServiceDisconnected (ComponentName名称){   mService=零;   isConnecting=false;   mTVStatus.setText(“已断开连接!”);   }   @Override   公共空间onServiceConnected (ComponentName名称、内部服务){   mService=new信使(服务);   isConnecting=true;   mTVStatus.setText(“连接成功!”);   }   };   公共空间解放(查看视图){   unbindService(康涅狄格州);   康涅狄格州=零;   }   @Override   保护无效onDestroy () {   unbindService(康涅狄格州);   康涅狄格州=零;   super.onDestroy ();   }   }      之前      

Android开发使用信使及处理程序进行通信的方法示例