Android跨进程通信之非AIDL(二)

目录

Android跨进程通信之小例子(一)
Android跨进程通信之非AIDL(二)
Android跨进程通信之Proxy与Stub(三)
Android跨进程通信之AIDL(四)

从最常用的例子谈起

这篇文章围绕的核心主题就是IBinder,或许你早就在ServiceonBind方法中见过。该方法就是返回一个IBinder对象。一般我们的做法是这样的:

service代码

public class MusicService extends Service{

    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    
        public class MyBinder extends Binder {
        public MyService getMyService() {
            return MusicService.this;
        }
    }
    
        public void play(){
            Log.i("TAG", "play");
        }
        public void pause(){
            Log.i("TAG", "pause");
        }
}

获取service控制权代码

public class MainActivity extends Activity {
    Intent              service;
    ServiceConnection   conn;
    MyService           myService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        service = new Intent(MainActivity.this, MusicService.class);
        conn = new ServiceConnection() {
            
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
            
            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                myService = ((MyBinder) binder).getMyService();
            }
        };
        bindService(service, conn, BIND_AUTO_CREATE);
    }
}

我们通过ServiceConnection去绑定一个Service然后绑定成功后返回一个IBinder对象,其中IBinder就充当与一个中介。是的,中介这个词没有用错。再通过MyBinder提供的getMyService来获取服务进而控制Service

一个真相引发的血案

其实上面的例子跟文章要说的内容没什么卵关系,只是让你更好的回忆一下IBinder接口。这篇文章主要讲一下onTransact方法。

IBinder跨进程通信图

ibinder

下面就大概简单说明一下。

  • 首先myActivity通过调用bindService去绑定一个远程的服务(myService),绑定成功后返回一个IBinder对象。这时候双方就算是建立了连接了。
  • 建立连接之后,双方就可以通过持有的IBinder进行通信。myActivity使用IBindertransact方法去给底层的Binder Driver(Linux层)发送消息间接调用底层的IBinderexecTransact方法。
  • execTransact导致的结果就是调用onTransact方法。那么这时候事件的处理就可以在该环节进行了。

其实就是一个transact->onTransact的一个过程

事实上我们看到transactonTransact的参数是一模一样的。

protected boolean onTransact(int code, Parcel data, Parcel reply,int flags);
public final boolean transact(int code, Parcel data, Parcel reply,int flags);

transact的参数

  • code : 这是一个识别码,类似于Handler体系里面的Message的what属性。根据不同的code产生不同的响应。
  • data : 调用transact的对象传送过去的参数。
  • reply : 调用onTransact的对象返回的参数。
  • flags : Java里面默认的native方法都是阻塞的,当不需要阻塞的时候设置为IBinder.FLAG_ONEWAY,否则设置为0

一个小例子

环境:

  • 一个叫做RemoteService的APP
  • 一个叫做RemoteServiceDemo的APP
  • RemoteServiceDemo中的Activity去控制RemoteService中的Service。

RemoteService

Service声明

<service android:name="com.example.remoteservice.MusicService" >
            <intent-filter>
                <action android:name="top.august1996.musicservice" />
            </intent-filter>
        </service>

别忘了加入<action android:name="top.august1996.musicservice" />,否则会抛出安全异常。

Service代码

public class MusicService extends Service {
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
    
    public class MyBinder extends Binder {
        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
            switch (code) {
                case 0:
                    Log.i("TAG", data.readString());
                    play();
                    reply.writeString("Service play Music at " + sdf.format(new Date()));
                    break;
                case 1:
                    Log.i("TAG", data.readString());
                    pause();
                    reply.writeString("Service pause Music at " + sdf.format(new Date()));
                    
                    break;
                default:
                    break;
            }
            
            return true;
        }
    }
    
    public void play() {
        Log.d("TAG", "play");
    }
    
    public void pause() {
        Log.d("TAG", "pause");
    }
}

服务的代码很简单,也没什么需要说的。

RemoteServiceDemo

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="bind"
        android:text="bind service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="unbind"
        android:text="unbind service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="play"
        android:text="play" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="pause"
        android:text="pause" />

</LinearLayout>

代码

public class MainActivity extends Activity {
    
    private ServiceConnection   mServiceConnection  = new ServiceConnection() {
                                                        @Override
                                                        public void onServiceDisconnected(ComponentName name) {
                                                            
                                                        }
                                                        @Override
                                                        public void onServiceConnected(ComponentName name,
                                                                IBinder service) {
                                                            mBinder = service;
                                                        }
                                                    };
    
    private IBinder             mBinder             = null;
    private Parcel              data                = Parcel.obtain();
    private Parcel              reply               = Parcel.obtain();
    private SimpleDateFormat    sdf                 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void bind(View v) {
        if (isBinded()) {
            return;
        }
        /**
         * 通过ComponentName去定位意图
         * 第一个参数是应用包名
         * 第二个参数是Service或者Activity所在的包名+类名
         */
        bindService(
                new Intent().setComponent(
                        new ComponentName("com.example.remoteservice", "com.example.remoteservice.MusicService")),
                mServiceConnection, Context.BIND_AUTO_CREATE);
    }
    
    public void unbind(View v) {
        if (!isBinded()) {
            return;
        }
        unbindService(mServiceConnection);
        mBinder = null;
    }
    
    public void play(View v) {
        if (!isBinded()) {
            return;
        }
        try {
            data.writeString("Activity request to play music at " + sdf.format(new Date()));
            mBinder.transact(0, data, reply, 0);
            Log.i("TAG", reply.readString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    public void pause(View v) {
        try {
            data.writeString("Activity request to pause music at " + sdf.format(new Date()));
            mBinder.transact(1, data, reply, 0);
            Log.i("TAG", reply.readString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    
    private boolean isBinded() {
        return mBinder != null;
    }
    
}

因为我们需要使用reply,所以我们使用阻塞的transact,否则我们调用了transact,但是那边还未给reply写入字符串,最终导致NullPointException。

项目地址

Github地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容