Android -- Messenger与Service
如果你需要你的service和其他进程通信,那么你可以使用一个Messenger来提供这个接口。
这种方法允许你在不使用 AIDL的情况下,进行跨进程通信IPC。
实现步骤
下面是一个如何使用 Messenger的小总结:
1. service实现一个 Handler 接收客户端每一次调用的回调。
2. Handler 用来创建一个Messenger对象,它是一个Handler的引用。
3. Messenger创建一个 IBinder,service从 onBind()中把它返回给客户端。
4. 客户端使用这个IBinder来实例化Messenger (service的Handler的引用),客户端使用它来向service发送Message对象。
5. service在它的Handler中接收每一个Message对象,在它的 handleMessage()方法中。
Code
public class MessengerService extends Service {/** Command to the service to display a message */static final int MSG_SAY_HELLO = 1;/*** Handler of incoming messages from clients.*/class IncomingHandler extends Handler{@Overridepublic void handleMessage(Message msg){switch (msg.what){case MSG_SAY_HELLO:Toast.makeText(getApplicationContext(), "hello!",Toast.LENGTH_SHORT).show();break;default:super.handleMessage(msg);}}}/*** Target we publish for clients to send messages to IncomingHandler.*/final Messenger mMessenger = new Messenger(new IncomingHandler());/*** When binding to the service, we return an interface to our messenger for* sending messages to the service.*/@Overridepublic IBinder onBind(Intent intent){Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();return mMessenger.getBinder();} }
注意 Handler中的 handleMessage() 方法是service接收到来的 Message并且决定做什么的地方。
客户端需要做的仅仅是创建一个基于service所返回的 IBinder的 Messenger,然后用 send()方法发送信息。
比如,这里有一个简单的activity和service绑定并且发送信息给service:
public class ActivityMessenger extends Activity {/** Messenger for communicating with the service. */Messenger mService = null;/** Flag indicating whether we have called bind on the service. */boolean mBound;/*** Class for interacting with the main interface of the service.*/private ServiceConnection mConnection = new ServiceConnection(){public void onServiceConnected(ComponentName className, IBinder service){// This is called when the connection with the service has been// established, giving us the object we can use to// interact with the service. We are communicating with the// service using a Messenger, so here we get a client-side// representation of that from the raw IBinder object.mService = new Messenger(service);mBound = true;}public void onServiceDisconnected(ComponentName className){// This is called when the connection with the service has been// unexpectedly disconnected -- that is, its process crashed.mService = null;mBound = false;}};public void sayHello(View v){if (!mBound)return;// Create and send a message to the service, using a supported 'what'// valueMessage msg = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);try{mService.send(msg);}catch (RemoteException e){e.printStackTrace();}}@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}@Overrideprotected void onStart(){super.onStart();// Bind to the servicebindService(new Intent(this, MessengerService.class), mConnection,Context.BIND_AUTO_CREATE);}@Overrideprotected void onStop(){super.onStop();// Unbind from the serviceif (mBound){unbindService(mConnection);mBound = false;}} }
注意这个例子并没有展示service如何响应客户端,如果你想要service响应,你需要在客户端中创建一个 Messenger。
然后当客户端接收到onServiceConnected()回调方法时,它会发送一个 Message到service,在它的send() 方法的replyTo参数中包含了客户端的Messenger。
我是天王盖地虎的分割线
本文转自我爱物联网博客园博客,原文链接:http://www.cnblogs.com/yydcdut/p/3950480.html,如需转载请自行联系原作者
Android -- Messenger与Service相关推荐
- android service交互,Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
- Android四大组件Service之AIDL详解
Android四大组件Service之AIDL详解 前言 简介 基础知识 AIDL 服务端 定义AIDL文件规则 创建 .aidl 文件 清单注册 通过 IPC 传递对象 调用 IPC 方法 Andr ...
- android Messenger 用法,demo,helloworld
如果你觉得写aidl比较麻烦,android还简单封装了一个 Messenger api,可以进行简单的跨进程通信,不过我干android这么多年,从来没有见过有哪个项目用过这玩意儿,因为太麻烦,还不 ...
- android notification 定时显示,Android编程使用Service实现Notification定时发送功能示例...
本文实例讲述了android编程使用service实现notification定时发送功能.分享给大家供大家参考,具体如下: /** * 通过启动或停止服务来管理通知功能 * * @descripti ...
- Android之旅---Service
Service介绍 首先让我们来假设这样一种情况:用户正在使用你的音乐播放器播放着优美的歌曲,突然用户听说日本现在9级地震了想去浏览器中看看相关的新闻.也就是说用户想边听音乐边看新闻.如果按照我们前面 ...
- 自动生成Android界面,面向Android的Web Service界面自动生成技术研究
摘要: 据统计,开发人员在开发应用程序的过程中,接近一半的代码用于用户界面部分,大约一半的运行时间用于执行这一部分.所以,减少用户界面部分的开发代码和运行时间,能有效提高程序的运行效率.智能家居中,由 ...
- Android 中的 Service 全面总结(转)
转自:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html# Android 中的 Service 全面总结 1.Service的种 ...
- Android学习笔记-Service
* 1.Service是一个应用程序组件 * 2.Service没有图形化界面 * 3.Service通常用来处理一些耗时比较长的操作 * 4.可以使用Service更新ContentProvider ...
- Android中的Service组件详解
Service与Activity的区别在于:Service一直在后台运行,他没有用户界面,绝不会到前台来. 一,创建和配置Service 开发Service需要两个步骤:1,继承Service子类,2 ...
最新文章
- python的安装教程-python安装教程 Pycharm安装详细教程
- 统考计算机2010年版,2010年计算机专业统考试题数据结构
- oracle打patch,Oracle初学者入门指南-How to get Oracle Patch?
- 步步深入MySQL:架构-gt;查询执行流程-gt;SQL解析顺序!
- 为什么每个邮件收到后都会有一个htm的附件_Python3.x 发送各种形式的告警邮件内容...
- ADS软件仿真的问题
- Redis 实战场景详解
- 解决Qt安装后没有桌面图标
- 基于AES的图像加密
- 2D图像像素点操作——平移,旋转,缩放 tcy
- 默认暴露,分别暴露,整体暴露的再次学习及常用知识
- 2016 安全行业全景图——By 安全牛
- markdown画图之一:流程图(flowchart)
- app软件开发现状及前景
- 小组取什么名字好_生日日期做网名,取什么名字好?
- 4.9-4.10 矩阵乘法的性质 矩阵的幂运算 矩阵的转置及其性质
- python 英语分词是什么意思_英语中分词是什么意思?
- cuda pytorch 环境变量_Windows10+CUDA 10.1.0+pytorch安装过程
- YC中国首场个路演日落幕,这22家企业凭什么脱颖而出?
- 以图搜图 相似图片搜索的原理(二)