之前看了一篇,然后不自己动手肯定是不行的,然后自己又写了一遍。
背景:
一般使用messenger进行进程间通信的时候,我们只能进行单方向通信。但是有没有办法让服务器端和客户端进行双向通信呢?
解决思路:
在第一次消息交互的时候,将客户端的消息队列作为一个对象传给服务端,让服务端取到客户端的消息队列。这样就可以实现双向通信了。
代码如下:
需要注意的是,远程绑定service的地方,需要加上包名,不要忘记。
重点关注红色部分,其余的地方没有什么好说的啦。
服务端代码
1 package com.example.xxx.server_testmsger; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 6 public class MainActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13 }
1 package com.example.xxx.server_testmsger; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaRecorder; 6 import android.os.Handler; 7 import android.os.IBinder; 8 import android.os.Message; 9 import android.os.Messenger; 10 import android.os.RemoteException; 11 import android.util.Log; 12 13 public class serverService extends Service { 14 private static final int MSG_WHAT = 1; 15 private static final int REQUEST_KEYEVENT = 1; 16 private static final int MSG_TOCLIENT = 2; 17 private static final int REQUEST_KEYEVENT_TOCLIENT = 2; 18 private Messenger mMessenger_from_client = null; 19 20 public serverService() { 21 } 22 23 private Messenger mMessager = new Messenger(new Handler() { 24 @Override 25 public void handleMessage(Message msg) { 26 Log.d("Server MSG", "handleMessage()"); 27 super.handleMessage(msg); 28 29 mMessenger_from_client = (Messenger)msg.obj; 30 Message message = msg.obtain(msg); 31 Log.d("Server MSG", message.toString()); 32 switch (message.what) { 33 case MSG_WHAT: 34 Message msgToClient = new Message(); 35 msgToClient.what=MSG_TOCLIENT; 36 msgToClient.arg1=REQUEST_KEYEVENT_TOCLIENT; 37 try { 38 Log.d("Server MSG", "sending message...."); 39 mMessenger_from_client.send(msgToClient); 40 } catch (RemoteException e) { 41 e.printStackTrace(); 42 } 43 break; 44 default: 45 break; 46 } 47 } 48 49 }); 50 51 @Override 52 public IBinder onBind(Intent intent) { 53 return mMessager.getBinder(); 54 } 55 }
客户端代码
1 package com.example.xxx.client_testmsger; 2 3 import android.content.ComponentName; 4 import android.content.Context; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Handler; 8 import android.os.IBinder; 9 import android.os.Message; 10 import android.os.Messenger; 11 import android.os.RemoteException; 12 import android.support.v7.app.AppCompatActivity; 13 import android.os.Bundle; 14 import android.util.Log; 15 import android.view.ContextThemeWrapper; 16 import android.view.View; 17 import android.widget.Button; 18 19 import java.io.InputStream; 20 21 public class MainActivity extends AppCompatActivity { 22 private Messenger mMessenger = null; 23 private Messenger mServiceMessenger = null; 24 private Intent serviceIntent; 25 private static final int MSG_WHAT = 1; 26 private static final int REQUEST_KEYEVENT = 1; 27 private static final int MSG_TOCLIENT = 2; 28 private static final int REQUEST_KEYEVENT_TOCLIENT = 2; 29 30 private Handler mHandler = new Handler() { 31 @Override 32 public void handleMessage(Message msg) { 33 super.handleMessage(msg); 34 switch (msg.what) { 35 case MSG_TOCLIENT: 36 Log.e("Client MSG","got Message from server"); 37 Log.e("Client MSG",msg.toString()); 38 break; 39 default: 40 break; 41 } 42 } 43 }; 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setContentView(R.layout.activity_main); 48 49 serviceIntent = new Intent(); 50 serviceIntent.setComponent(new ComponentName("com.example.xxx.server_testmsger","com.example.xxx.server_testmsger.serverService")); 51 bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE); 52 mMessenger = new Messenger(mHandler); 53 } 54 55 private ServiceConnection mConnection = new ServiceConnection() { 56 @Override 57 public void onServiceConnected(ComponentName name, IBinder service) { 58 Log.e("MSG","MainActivity Start"); 59 Log.d("Client MSG", "onServiceConnected()"); 60 if (service == null) { 61 mServiceMessenger = null; 62 } else { 63 mServiceMessenger = new Messenger(service); 64 Message message = new Message(); 65 message.what = MSG_WHAT; 66 message.arg1 = REQUEST_KEYEVENT; 67 message.obj = mMessenger; 68 try { 69 Log.d("Client MSG", "Message send()"); 70 mServiceMessenger.send(message); 71 } catch (RemoteException e) { 72 e.printStackTrace(); 73 } 74 } 75 } 76 77 @Override 78 public void onServiceDisconnected(ComponentName name) { 79 Log.d("Client MSG", "onServiceDisconnected()"); 80 mServiceMessenger = null; 81 } 82 }; 83 84 @Override 85 public void onDestroy() { 86 super.onDestroy(); 87 unbindService(mConnection); 88 } 89 }