Service

作者: 咚咚_Coding | 来源:发表于2023-04-13 16:18 被阅读0次

    1、跨进程Messager 多应用间单线程

    把Messenger比喻为一个信使,信使的作用是派信;那么Message就比喻为信件、信封,即信使派的东西;那么Bundle是什么呢?Message里面保存了Bundle,那么bundle可以比喻为信纸,信纸上写满了各种我们要传递的信息。读到这里,读者应该明白了这三者在Messenger通讯方式内所扮演的角色了。简单来说:Messenger把装有Bundle的Message发送到别的进程。
    ###客户端
    private Messenger clientMessenger = new Messenger(handler);
    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            Log.i(TAG,"收到远程服务数据:"+bundle.getString("service"));
        }
    };
    
    private void bindRemoteService(){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.xxx",
                "com.example.xxx.MessengerService"));
        boolean isBind = bindService(intent,conn, Service.BIND_AUTO_CREATE);
        Log.i(TAG,"bindRemoteService isBind="+isBind);
    }
    private Messenger messengerProxy;
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder iBinder) {
            Log.i(TAG,"onServiceConnected:");
            messengerProxy = new Messenger(iBinder);
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG,"onServiceDisconnected:");
        }
    };
    
    private void sendData(){
        if (messengerProxy == null) return;
        Message message = new Message();
        message.replyTo = clientMessenger;
        Bundle bundle = new Bundle();
        bundle.putString("client","这是客户端发送数据:"+messageid);
        message.setData(bundle);
        messengerProxy.send(message);
       }
    
    ###服务端
    //创建服务端Messenger
    private final Messenger mMessenger = new Messenger(messengerHandler);
    
    @Override
    public IBinder onBind(Intent intent) {
        //向客户端返回IBinder对象,客户端利用该对象访问服务端
        return mMessenger.getBinder();
    }
      //拿到客户端的message 发送消息     
      private  Handler messengerHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //取出客户端的消息内容
            Bundle bundle = msg.getData();
            String clientMsg = bundle.getString("client");
            Log.i(TAG, "来自客户端的消息:" + clientMsg);
            //新建一个Message对象,作为回复客户端的对象
            Message message = Message.obtain();
            Bundle bundle1 = new Bundle();
            bundle1.putString("service", clientMsg+",服务端收到");
            message.setData(bundle1);
            try {
                msg.replyTo.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };  
    

    2、AIDL 多应用间多线程


    参数定向tag
    

    image.png image.png image.png

    相关文章

      网友评论

          本文标题:Service

          本文链接:https://www.haomeiwen.com/subject/oviaddtx.html