Binder 进程间的通信---ipc通信

其实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中,你会发现,其实serviceservice直接也就是两个进程直接可以通信,这种ipc机制经常用于音乐播放器。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RockTong_yuhui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值