其实IPC通信就是进程之间的通信。与线程之间的通信不同,线程间通信是在一个进程间不同线程的通信。
而IPC通信是跨进程的,通信双方完全可能在两个应用中。下面我在一个应用中的不同进程中实现IPC。
一、原理图
二、代码实现如下:
1、MainActivity.Java
package com.example.testipc;
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.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity{
publicstatic final int SAY_HELLO_TO_CLIENT = 0;
publicServiceConnection mSc;
classIncomingHandler extends Handler{
@Override
publicvoid handleMessage(Message msg) {
switch(msg.what){
caseSAY_HELLO_TO_CLIENT:
Log.d("rocktong","Hello World Remote Client!");
break;
default:
super.handleMessage(msg);
}
}
}
Messengermessenger_reciever = new Messenger(new IncomingHandler());
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSc= new ServiceConnection(){
@Override
publicvoid onServiceConnected(ComponentName arg0, IBinder service) {
//TODO Auto-generated method stub
Messengermessenger = new Messenger(service);
Messagemsg = new Message();
msg.what= RemoteService.MSG_SAY_HELLO;
msg.replyTo= messenger_reciever;
try{
messenger.send(msg);
}catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
publicvoid onServiceDisconnected(ComponentName arg0) {
Log.d("rocktong","service disconnected");
}};
}
@Override
protectedvoid onStart() {
super.onStart();
Log.d("rocktong",this.getApplicationContext().getPackageCodePath());
Intent service = newIntent(this.getApplicationContext(),RemoteService.class);
this.bindService(service, mSc,Context.BIND_AUTO_CREATE);
}
@Override
protectedvoid onStop() {
super.onStop();
//must unbind the service otherwise theServiceConnection will be leaked.
this.unbindService(mSc);
}
}
2、RemoteService.java
package com.example.testipc;
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.util.Log;
import android.widget.Toast;
public class RemoteService extends Service{
publicstatic final int MSG_SAY_HELLO = 0;
HandlerIncomingHandler = new Handler(){
@Override
publicvoid handleMessage(Message msg) {
if(msg.replyTo!= null){
Messagemsg_client = this.obtainMessage();
msg.what= MainActivity.SAY_HELLO_TO_CLIENT;
try{
((Messenger)msg.replyTo).send(msg_client);
}catch (RemoteException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
switch(msg.what){
caseMSG_SAY_HELLO:
Log.d("rocktong","Hello World Remote Service--------------------------------------!");
break;
default:
super.handleMessage(msg);
}
}
};
Messengermessager = new Messenger(IncomingHandler);
@Override
publicIBinder onBind(Intent arg0) {
returnmessager.getBinder();
}
}
3、 AndroidManifest.xml
<?xml version="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testipc"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testipc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<actionandroid:name="android.intent.action.MAIN" />
<categoryandroid:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RemoteService"android:process=":remote"></service>
</application>
</manifest>
4、总结:
从这个简单的IPC demo中,你会发现,其实service与service直接也就是两个进程直接可以通信,这种ipc机制经常用于音乐播放器。