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
{
@Override
public 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.
*/
@Override
public 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'
// value
Message msg = Message
.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
try
{
mService.send(msg);
}
catch (RemoteException e)
{
e.printStackTrace();
}
} @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} @Override
protected void onStart()
{
super.onStart();
// Bind to the service
bindService(new Intent(this, MessengerService.class), mConnection,
Context.BIND_AUTO_CREATE);
} @Override
protected void onStop()
{
super.onStop();
// Unbind from the service
if (mBound)
{
unbindService(mConnection);
mBound = false;
}
}
}
注意这个例子并没有展示service如何响应客户端,如果你想要service响应,你需要在客户端中创建一个 Messenger。
然后当客户端接收到onServiceConnected()回调方法时,它会发送一个 Message到service,在它的send() 方法的replyTo参数中包含了客户端的Messenger。
我是天王盖地虎的分割线
参考:http://www.cnblogs.com/mengdd/tag/Android/default.html?page=4
Android -- Messenger与Service的更多相关文章
- (转载)Android中的Service:Binder,Messenger,AIDL(2)
前言 前面一篇博文介绍了关于Service的一些基本知识,包括service是什么,怎么创建一个service,创建了一个service之后如何启动它等等.在这一篇博文里有一些需要前一篇铺垫的东西,建 ...
- (转载)所有分类 > 开发语言与工具 > 移动开发 > Android开发 Android中的Service:默默的奉献者 (1)
前言 这段时间在看一些IPC相关的东西,这里面就不可避免的要涉及到service,进程线程这些知识点,而且在研究的过程中我惊觉自己对这些东西的记忆已经开始有些模糊了——这可要不得.于是我就干脆花了点心 ...
- Android 面试题--Service
1.Service 是否在 main thread 中执行, service 里面是否能执行耗时的操作?默认情况,如果没有显示的指 servic 所运行的进程, Service 和 activity ...
- Android探索之Service全面回顾及总结
什么是Service? Service(服务)是Android提供的四大组件之一,是一个没有用户界面的在后台运行执行耗时操作的应用组件.其他应用组件能够启动Service,并且当用户切换到另外的应用场 ...
- Android 中的 Service 全面总结(转载)
转载地址:http://www.cnblogs.com/newcj/archive/2011/05/30/2061370.html 感谢作者 Android 中的 Service 全面总结 1.Ser ...
- java攻城狮之路(Android篇)--BroadcastReceiver&Service
四大组件:activity 显示. contentProvider 对外暴露自己的数据给其他的应用程序.BroadcastReceiver 广播接收者,必须指定要接收的广播类型.必须明确的指定acti ...
- 图解Android - Binder 和 Service
在 Zygote启动过程 一文中我们说道,Zygote一生中最重要的一件事就是生下了 System Server 这个大儿子,System Server 担负着提供系统 Service的重任,在深入了 ...
- Android 服务类Service 的详细学习
http://blog.csdn.net/vipzjyno1/article/details/26004831 Android服务类Service学习四大组建 目录(?)[+] 什么是服务 服务有 ...
- 大仙说道之Android studio实现Service AIDL
今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...
随机推荐
- 【转载】ARouter-万能路由协议
Github源码地址:https://github.com/alibaba/ARouter 一.功能介绍 支持直接解析标准URL进行跳转,并自动解析参数注入 支持多模块工程使用 支持添加多个拦截器,自 ...
- [hdu3934] 凸包 旋转卡壳
大致题意: 求多边形的最大内接三角形 旋转卡壳 模板题 #include<cstdio> #include<iostream> #include<cstring> ...
- C#多线程编程实战(一):线程基础
1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...
- codevs 1392 合并傻子
1392 合并傻子 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个园形操场的四周站着N个傻子,现要将傻子有 ...
- 范浩强treap——可持久化
当平衡树需要可持久化的时候,意味着我们需要访问以前的某个时间点的平衡树,就要保持以前的树形态不变,新建一个时间戳,构建一棵新的树. 如果用以前的旋转treap可能就不方便做到(又要打时间戳,又要新建节 ...
- 【HDU】6410:序列期望
序列期望 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- [转]android中OnTouch和OnClick、 imagebutton的src和background有什么区别
1.OnTouch和OnClick有何区别? 专业回答 1.onClick就传入一个View对象,而 onTouch要传入一个View 对象和 MotionEvent的对象2.onTouch对控件 ...
- HDU 5646 DZY Loves Partition 数学 二分
DZY Loves Partition 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5646 Description DZY loves parti ...
- hdu 4119 Isabella's Message 模拟题
Isabella's Message Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
- 也谈时间管理和GTD
也谈时间管理和GTD 时间管理 随着事情越来约多发现时间越来越不够用了,但是其实每天时间都是恒定的,并不增也不减,所以感觉时间不够用了总归只是个人主观感觉. 对我个人帮助比较大的是三本书<番茄时 ...