android Messenger跨进程通信
Posted 踏雪羽翼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android Messenger跨进程通信相关的知识,希望对你有一定的参考价值。
1、android messager可以实现不同进程之间数据的传递。通过点击进去看messenger类可以看到,我们可以创建一个service用于处理和客户端的连接请求,然后通过handler中创建一个messener对象,实现对service数据传递。/**
* Create a new Messenger pointing to the given Handler. Any Message
* objects sent through this Messenger will appear in the Handler as if
* @link Handler#sendMessage(Message) Handler.sendMessage(Message) had
* been called directly.
*
* @param target The Handler that will receive sent messages.
*/
public Messenger(Handler target)
mTarget = target.getIMessenger();
/**
* Create a Messenger from a raw IBinder, which had previously been
* retrieved with @link #getBinder.
*
* @param target The IBinder this Messenger should communicate with.
*/
public Messenger(IBinder target)
mTarget = IMessenger.Stub.asInterface(target);
通过getBinder可以在service获取到发送过来的数据
public IBinder getBinder()
return mTarget.asBinder();
通过send方法发送数据
public void send(Message message) throws RemoteException
mTarget.send(message);
2、下面实现不同进程之间的数据传递,首先在androidmanifest.xml中添加下面代码,第二句的意思时给service开一个新的进程,:开头书属于当前应用的私有进程,其他应用是不能跟他跑在同一个进程中的,当不以:开头的命名是属于全局进程,其他应用是可以通过ShareUID方式跟它在同一个进程中。
<service android:name=".MessengerService"
android:process=":remote"/>
3、新建一个service类用于继承service,然后写一个Handler来首收发数据,当接受到发送过来的数据时,就再返回数据。
public class MessengerService extends Service
private final static String TAG = "MessengerService";
public final static int SERVICE_TEST = 21;
private final Messenger mMessenger = new Messenger(new MessengerHandler());
private static class MessengerHandler extends Handler
@Override
public void handleMessage(Message msg)
switch (msg.what)
case MainActivity.TEST_MESSAGE:
Log.e(TAG,"receiver msg is "+msg.getData().getString("msg")+" current time="+System.currentTimeMillis());
sendMsg(msg);
break;
default:
super.handleMessage(msg);
super.handleMessage(msg);
public static void sendMsg(Message msg)
Messenger service = msg.replyTo;
Message message = Message.obtain(null,SERVICE_TEST);
Bundle bundle = new Bundle();
bundle.putString("reply","test 回复你");
message.setData(bundle);
try
service.send(message);
catch (Exception e)
e.printStackTrace();
@Nullable
@Override
public IBinder onBind(Intent intent)
return mMessenger.getBinder();
4、首先可以先绑定messengerService,然后在里面进行数据的发送
private Messenger mMessenger;
private ServiceConnection mConnection = new ServiceConnection()
@Override
public void onServiceConnected(ComponentName name, IBinder service)
mMessenger = new Messenger(service);
Message msg = Message.obtain(null, TEST_MESSAGE);
Bundle bundle = new Bundle();
bundle.putString("msg", "test hehhehhehe");
msg.setData(bundle);
msg.replyTo = testMessenger;
try
mMessenger.send(msg);
catch (Exception e)
e.printStackTrace();
@Override
public void onServiceDisconnected(ComponentName name)
;
然后在启动onCreate中启动service
Intent intent = new Intent(getApplication(), MessengerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
同时在onDestroy中取消掉service
unbindService(mConnection);
这样我们就可以在activity中跨进程发送数据给service端,下面时打印的数据,可以看到它的进程时跟activity的是不一样的
service的进程
5、要使com.example.apple.messager进程中能接收到service发送过来的数据需要跟service一样写一个Handler 和Messenger用于接收数据
private Messenger testMessenger = new Messenger(new MessengerHandler());
private static class MessengerHandler extends Handler
@Override
public void handleMessage(Message msg)
switch (msg.what)
case MessengerService.SERVICE_TEST:
Log.e(TAG, "receiver service msg is " + msg.getData().getString("reply") + " current time="+System.currentTimeMillis() + " =="+msg.what);
break;
default:
super.handleMessage(msg);
这样就可以实现不同进程之间的数据收发了。现在是使用了service代替应用,其实可以写成不同应用直接实现数据的传递的,具体实现可自行修改。
例子代码下载地址: http://download.csdn.net/download/u011324501/9996821
以上是关于android Messenger跨进程通信的主要内容,如果未能解决你的问题,请参考以下文章