11111111111112

进程间通信之Messager

1、新建一个project,作为Service Module

Activity代码:不做变动

package com.pzhihao.textmessager;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

新建一个service,和其他进程通信

服务端的信使通过onBind方法return的getBinder传递到客户端的ServiceConnection中,同时,服务端得到了客户端通过服务端messager传递过来的客户端messager,服务端用来给客户端回传数据

Myservice.java

package com.pzhihao.textmessager;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class MyService extends Service {
    private static final int MSG_FROM_CLIENT = 1 ;
    private static final int MSG_FROM_SERVER = 2 ;
    private static final String TAG = "MyService";

    public MyService() {
    }

    Messenger messenger=new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case MSG_FROM_CLIENT:
                    Bundle data = msg.getData();
                    String msg1 = data.getString("clientmsg");
                    Log.v(TAG,"收到客户端消息"+msg1);

                    //得到客户端的信使
                    Messenger clientmessenger = msg.replyTo;
                    //封装消息
                    Message replymsg=new Message();
                    Bundle bundle=new Bundle();
                    bundle.putString("servermsg","收到了您的消息!");
                    replymsg.setData(bundle);
                    replymsg.what=MSG_FROM_SERVER;
                    try {
                        //用客户端的信使给客户端发消息
                        clientmessenger.send(replymsg);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    });

    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}

AndroidManifest.xml片段,服务注册

<service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="pzhihao.Myservice"></action>
            </intent-filter>
        </service>

 

 

2、新建一个Module,作为客户端,向上一个app索取服务,客户端通过bind_service方法向服务端传递了数据,同时也将自己的messager传递了过去

ManiActivity.java

package com.pzhihao.client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final int MSG_FROM_CLIENT = 1 ;
    private static final int MSG_FROM_SERVER = 2 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent=new Intent();
        intent.setAction("pzhihao.Myservice");
        intent.setPackage("com.pzhihao.textmessager");

        bindService(intent,conn,BIND_AUTO_CREATE);

    }
    private Messenger mservice;

    private Messenger clicntmessager=new Messenger(new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_FROM_SERVER:
                    Bundle data = msg.getData();
                    String servermsg = data.getString("servermsg");
                    Log.v("MainActivity","客户端收到服务端的反馈:"+servermsg);
                    break;
            }
        }
    });

    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mservice=new Messenger(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void bind_sevice(View view) {

        Message msg=Message.obtain();
        Bundle bu=new Bundle();
        bu.putString("clientmsg","客户端传来的消息");
        msg.setData(bu);
        msg.what=MSG_FROM_CLIENT;
        //客户端从Service得到服务端的Messenger后,给服务端传递了消息,并把自己的Messenger带了过去
        msg.replyTo=clicntmessager;
        try {
            mservice.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }
}

 

posted @ 2016-08-11 20:41  颤抖的飞机  阅读(389)  评论(0)    收藏  举报
111 11222