当前位置: 首页 > 工具软件 > Messenger > 使用案例 >

messenger简单说明

诸葛乐逸
2023-12-01

messenger(信使) :即送信者

messenger通信即通过信使在不同进程中通信

现简单说明(采用1对1通信对于多对多建议使用aidl,messenger具有延时性):   客户端    与服务端

1对1中信使有2个    clientMessenger  与serviceMessenger

在android中一般都是通过服务来使2个不同的进程通信

当客户端绑定远程服务端时此时会有一个IBinder对象通信,此时可以借助这个对象在服务端指定handler(有消息传过来必须要有相应的处理者来处理消息)(此时一般做法是用handler来构造messenger,messenger.getBinder可以获得指定的IBinder,然后在Onbind把对象传到客户端)

客户端获得通信接口之后就可以获得通过Messenger构造获得serviceMessenger   记得客户端回信要把客户端信使回复过去  这样服务端就可以通过客户端信使回信,从而达到相互通信的目的

 

 

简单例子:

客户端:

public class MainActivity extends AppCompatActivity {
    private static final String TAG ="*****";
//标志位  是否已经连接
    private  boolean isConnection = false;
//服务端信息处理者
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what ==1&&msg.arg1 ==111) Log.d(TAG, "handleMessage: =我已经收到了服务端你的消息");
            MainActivity.this.finish();
        }
    };
    private Messenger clientMassenger=  new Messenger(mHandler);
    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBinder = service;
            Messenger serviceMassenger = new Messenger(service);//通过BInder对象构造服务端Messenger

//回信内容
            Bundle bundle = new Bundle();
            bundle.putString("message","connection init");
            Message message = new Message();
            message.setData(bundle);
//把自身的信使告诉服务端
            message.replyTo = clientMassenger;
            try {
//用服务端信使回信
                serviceMassenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            MainActivity.this.isConnection = true;
            Log.d(TAG, "onServiceConnected: ");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            MainActivity.this.isConnection = false;
        }
    };
    private IBinder mBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("远程服务端包名","远程服务端包名.MyService");
        intent.setComponent(componentName);
        bindService(intent,mServiceConnection,Service.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MainActivity.this.isConnection = false;
//记得解绑  不然  远程服务不会关掉
        unbindService(mServiceConnection);
    }
}

 

 

服务端:

public class MyService extends Service {
    private static final String TAG="****";
    private String whatSay = null;
//客户端回信处理者
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
//记住客户端信使,以后还要通过它回信
            clientMessenger = msg.replyTo;
            Log.d(TAG, "handleMessage: services");
            whatSay = msg.getData().getString("message");
            say();
//指定回信内容
            Message message = new Message();
            message.what = 1;
            message.arg1 =111;
            try {
                clientMessenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            super.handleMessage(msg);
        }

    };
//通过Handle构造自身的信使
    private Messenger serviceMessenger = new Messenger(mHandler);
    private Messenger clientMessenger = null;
    public MyService() {
    }
    private void say(){
        Log.d(TAG, "say: ="+whatSay);
    }
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        // TODO: Return the communication channel to the service.
//通过信使获得通信接口
        return serviceMessenger.getBinder();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: services");
    }
}
 类似资料: