zoukankan      html  css  js  c++  java
  • Messenger类的使用

    一、Messenger类

    作用:类似Message类,但是是跨进程使用的。

    解析:它的底层是由AIDL实现的,从构造方法可以看出

           //Service使用

            public Messenger(Handler target){

                 mTarget = target.getIMessenger();   

            }

           //Client端使用

            public Messenger(IBinder targer){

              mTarget = IMessenger.Stub.asInterface(target);

            }

    二、使用(Service与Client互相发送信息)

    步骤一:Service端创建Messenger

    public class BookService extends Service {
        private static final String TAG = "BookService";
        //①:创建Handler,为创建Messenger做准备
        private Handler mHandler = new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 0:
                        //当客户端发送信息的时候吗,输出
                Bundle bundle = msg.getData(); Log.d(TAG,"Service收到消息"+bundle.getString("msg")); break; } } }; //②:创建Messenger private Messenger mMessenger = new Messenger(mHandler); @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { //③:将Messenger对象的Binder返回给Client return mMessenger.getBinder(); } }

    步骤二:Client端接收Messenger发送信息给Service端

    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity";
    
        private Messenger mService;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ServiceConnection connection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    //重新创建Messenger
                    mService = new Messenger(service);
                    //从Msg池中获取message
                    Message msg = Message.obtain(null,0);
                    //创建Bundle
                    Bundle bundle = new Bundle();
                    bundle.putString("msg","Hello,nice to meet you");
                    //将Bundle装进message对象中
                    msg.setData(bundle);
                    try {
                        //将message发送给服务端,同时说明Message继承了Parcelable接口
                        mService.send(msg);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
    
                }
    
                @Override
                public void onServiceDisconnected(ComponentName name) {
    
                }
            };
            Intent intent = new Intent(this,BookService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }

    步骤三:实现服务器向客户端发送信息的双向交流

    //Client端
    
    /**
    *第一步:创建Handler接收服务器发送过来的信息
    */
        private Handler mHandler = new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 0:
                        Bundle bundle = msg.getData();
                        Log.d(TAG,"客户端接收到信息:"+bundle.getString("replyData"));
                        break;
                }
            }
        };
    
    /**
    *第二步:在 public void onServiceConnected(ComponentName name, IBinder service) 方法中添加
    */
    msg.replyTo = mService;
    /**
    *Service端的Handler中
    */
     //在Handler中接收到Client中传送过来的Messenger
            Messenger messenger = msg.replyTo;
             //创建Msg
             Message replyMessage = Message.obtain(null,0);
             try {
               //将Msg发送回给Client
              messenger.send(replyMessage);
             } catch (RemoteException e) {
                     e.printStackTrace();
             }


    流程图

  • 相关阅读:
    MySQL 5.6.9 RC 发布
    红薯 Java 8 的日期时间新用法
    Couchbase Server 2.0 发布,NoSQL 数据库
    Firefox OS 模拟器 1.0 发布
    Calculate Linux 13 Beta 1 发布
    敏捷测试的团队构成
    Node.js 0.8.16 发布(稳定版)
    JASocket 1.1.0 发布
    Samba 4.0 正式版发布,支持活动目录
    Seafile 1.3 发布,文件同步和协作平台
  • 原文地址:https://www.cnblogs.com/rookiechen/p/5352264.html
Copyright © 2011-2022 走看看