Android信使Messenger范例源码
来源:互联网 发布:excel多张表格数据汇总 编辑:程序博客网 时间:2024/04/25 01:41
Messenger是Android提供的一个工具类,主要为了简化进程间通信的功能。使用Messenger就可以避免编写AIDL文件来进行进程间通信。Messenger和Handler一起配合完成任务。其中Messenger主要用来将服务端和客户端的两个信使关联到一起,而Handler则用来完成具体功能。
具体代码如下:
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来定位远程服务并连接。
0 0
- Android信使Messenger范例源码
- Android中的信使Messenger的源码解析
- Android信使Messenger解析
- Messenger(信使)
- Messenger--信使
- Android应用进程间通信之Messenger信使使用及源码浅析
- Android应用进程间通信之Messenger信使使用及源码浅析
- Android应用进程间通信之Messenger信使使用及源码浅析
- Android应用进程间通信之Messenger信使使用及源码浅析
- Android应用进程间通信之Messenger信使使用及源码浅析
- 信使服务 (Messenger)
- Tokonda Messenger(信使)
- RemoteService中的 Messenger信使
- Android 绑定类型服务---使用信使(Messenger)
- Android 绑定类型服务---使用信使(Messenger)
- Android开发之Messenger信使和service实现跨进程
- Android进程间通信之--Messenger(信使)
- Android Messenger源码分析
- Thinking in Java---从哲学家就餐问题看死锁现象
- Arduino接口-week4-Lesson1
- 构建高并发高可用的电商平台架构实践
- 分布式视频编码研究第一天PRISM视频编解码
- C#设计模式——策略模式
- Android信使Messenger范例源码
- 遇到字符串操作时谨记
- HTML-03表单的创建
- centos7下恢复网卡名字eth
- HDU 2089 不要62
- 网络游戏到底该服务大众玩家还是大R?
- 【2016】一月份英语学习总结
- coredump详解
- Gesture手势-------LeapMotion API