Android信使Messenger范例源码
2016-02-02 21:28
483 查看
Messenger是Android提供的一个工具类,主要为了简化进程间通信的功能。使用Messenger就可以避免编写AIDL文件来进行进程间通信。Messenger和Handler一起配合完成任务。其中Messenger主要用来将服务端和客户端的两个信使关联到一起,而Handler则用来完成具体功能。
具体代码如下:
http://download.csdn.net/detail/logicteamleader/9426067
服务端的Messenger需要在onBind方法中返回IBinder实例,例如:
客户端则需要在如下代码中创建与之关联的客户端Messenger:
两边的信使通过IBinder实例关联起来后,就可以通过send方法来互相发送消息了。
如果需要回调,则将另一个Messenger放到Message的replayTo属性中发送给另一端,由另一端来执行回调函数。
服务端代码:
客户端代码:
layout代码:
Manifest代码:
最后说明:为了简单起见,我把服务端和客户端做到一个app中了。如果分开做,则需要使用Service的Action来定位远程服务并连接。
具体代码如下:
http://download.csdn.net/detail/logicteamleader/9426067
服务端的Messenger需要在onBind方法中返回IBinder实例,例如:
@Override public IBinder onBind(Intent intent) { return messenger.getBinder(); }
客户端则需要在如下代码中创建与之关联的客户端Messenger:
@Override public void onServiceConnected(ComponentName name, IBinder service) { // 得到了一个binder作为桥梁,创建客户端的信使 messenger = new Messenger(service); }
两边的信使通过IBinder实例关联起来后,就可以通过send方法来互相发送消息了。
如果需要回调,则将另一个Messenger放到Message的replayTo属性中发送给另一端,由另一端来执行回调函数。
服务端代码:
package com.dumaisoft.wxbmessengertest; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.widget.Toast; public class MessengerService extends Service { public static final int WXB_MSG_SERVER_1 = 0x0001; public static final int WXB_MSG_SERVER_CALLBACK = 0x0000; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch(msg.what){ case WXB_MSG_SERVER_1: Toast.makeText(MessengerService.this, "WXB_MSG_1 in Server", Toast.LENGTH_LONG).show(); break; case WXB_MSG_SERVER_CALLBACK: //构造消息,进行回调,让客户端信使的Handler来执行 Message message = Message.obtain(); message.what = MainActivity.WXB_MSG_CLIENT_1; try { msg.replyTo.send(message); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } super.handleMessage(msg); } }; private Messenger messenger = new Messenger(handler); @Override public IBinder onBind(Intent intent) { return messenger.getBinder(); } }
客户端代码:
package com.dumaisoft.wxbmessengertest; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { public static final int WXB_MSG_CLIENT_1 = 0x0010; private Button btnBind, btnUnbind, btnMessage,btnCallback; private TextView tv; private boolean isBind = false; private Messenger messenger; //clientMessenger的handler private Handler clientHandler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what == WXB_MSG_CLIENT_1){ Toast.makeText(MainActivity.this, "WXB_MSG_CLIENT_1 in Client", Toast.LENGTH_LONG).show(); } super.handleMessage(msg); } }; //用来在客户端回调服务端请求的Messenger private Messenger clientMessenger = new Messenger(clientHandler); private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { // 得到了一个binder作为桥梁,创建客户端的信使 messenger = new Messenger(service); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.tvBind); btnBind = (Button) this.findViewById(R.id.bindBtn); btnBind.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent service = new Intent(); service.setClass(MainActivity.this, MessengerService.class); bindService(service, connection, Context.BIND_AUTO_CREATE); isBind = true; tv.setText("Service is bind"); } }); btnUnbind = (Button) this.findViewById(R.id.unbindBtn); btnUnbind.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { unbindService(connection); isBind = false; tv.setText("Service is unbind"); } }); btnMessage = (Button) this.findViewById(R.id.msgBtn); btnMessage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isBind) { Message msg = Message.obtain(); msg.what = MessengerService.WXB_MSG_SERVER_1; try { messenger.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } } }); btnCallback = (Button) this.findViewById(R.id.callbackBtn); btnCallback.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isBind) { Message msg = Message.obtain(); msg.what = MessengerService.WXB_MSG_SERVER_CALLBACK; //设置回调的信使 msg.replyTo = clientMessenger; try { messenger.send(msg); } catch (RemoteException e) { e.printStackTrace(); } } } }); } }
layout代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.dumaisoft.wxbmessengertest.MainActivity" > <TextView android:id="@+id/tvBind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="service is unbind" /> <Button android:id="@+id/bindBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:text="bind" /> <Button android:id="@+id/unbindBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/bindBtn" android:layout_below="@+id/bindBtn" android:text="unbind" /> <Button android:id="@+id/msgBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/unbindBtn" android:text="message" /> <Button android:id="@+id/callbackBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/msgBtn" android:layout_below="@+id/msgBtn" android:text="callback" /> </RelativeLayout>
Manifest代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dumaisoft.wxbmessengertest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="20" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="MessengerService" android:process=":remote"> <intent-filter> <action android:name="com.dumaisoft.wxbmessengertest"/> </intent-filter> </service> </application> </manifest>
最后说明:为了简单起见,我把服务端和客户端做到一个app中了。如果分开做,则需要使用Service的Action来定位远程服务并连接。
相关文章推荐
- Android学习 RadioButton
- SimpleNews- Android MVP模式学习
- 导入AndroidStudio的项目卡死,如何快速导入AndroidStudio的项目
- 【官方】Android Studio中使用Git托管到GitHub
- 【Android开发小记--4】动画--线性布局动画
- Android进阶自定义控件之五子棋盘
- html中的table在android端显示
- 在mac 上配置AndroidStudio碰到的坑
- android *** Layout 13 ScrollView
- Android ViewPager刷新解析
- Android Paint类的
- android 传感器 光照传感器示例
- Android使用AttributeSet自定义控件的方法
- android Theme使用三
- Android AdapterView View的复用机制 分析
- Windows XP环境下 搭建Android NDK环境
- Android Studio集成Genymotion模拟器
- Android Unable to resolve target 'android-X' 解决方法
- android studio导入第三方库引发的问题和解决方法
- touchmove在android下面只触发一次的奇葩设定