Android IPC进程间通信之AIDL双向通信

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/83414600

Android IPC进程间通信之AIDL双向通信

假设现在有两个App,一个是作为服务器端的App称之为:Server。另一个是作为客户端端的App称之为:Client。
Server对外提供计算服务,为连接上的客户端端App:Client提供计算服务,Server这个App接受客户端App:Client传递过来的两个整数,然后服务器端App:Server负责计算求和,然后Server把计算结果传回给客户端App:Client。

先实现服务器端App:Server的代码:

先定义aidl代码文件中的接口ServerAIDLInterface.aidl:

// ServerAIDLInterface.aidl
package zhangphil.book.server;

interface ServerAIDLInterface {
    int add(int a,int b);
}

函数add()即为服务器端App打算公开暴露、提供给客户端App跨进程访问和使用的函数接口。

ServerStub继承自ServerAIDLInterface中的Stub,ServerStub.java:

package zhangphil.book.server;

import android.os.RemoteException;

public class ServerStub extends ServerAIDLInterface.Stub {
    @Override
    public int add(int a, int b) throws RemoteException {
        return a + b;
    }
}

ServerStub真正实现了服务器端要暴露给客户端跨进程访问的函数接口实现。

服务器端需要再写一个服务常驻提供对外服务,ServerService.java:

package zhangphil.book.server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class ServerService extends Service {
    private ServerStub mServerStub = new ServerStub();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mServerStub;
    }
}

需要在Androidmanifest.xml定义、声明这个服务:

        <service android:name="zhangphil.book.server.ServerService">
            <intent-filter>
                <action android:name="start_zhangphil_book_server_service_action" />
            </intent-filter>
        </service>

整个服务器端代码组织结构如图:

以上服务器端App:Server代码完成。

####################################################################

####################################################################

以下是客户端App:Client的代码:

客户端要完成的代码比较轻量级。首先需要把服务器端App:Server自动生产的ServerAIDLInterface.java备份到Client的代码文件中。

然后直接连接服务器端App:Server,传递两个随机生产的整数,让服务器端App:Server计算,计算完毕输出即可:

package zhangphil.book.client;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import zhangphil.book.server.ServerAIDLInterface;

public class MainActivity extends AppCompatActivity {

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ServerAIDLInterface mServerAIDLInterface = ServerAIDLInterface.Stub.asInterface(service);

            try {
                int a = (int) (Math.random() * 10);
                int b = (int) (Math.random() * 10);

                //客户端进程请求服务器端进程计算两个数a和b的结果。
                Log.d("计算", a + " + " + b);
                int result = mServerAIDLInterface.add(a, b);

                //来自服务器端进程的计算结果
                Log.d("计算", "服务端端计算结果:" + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent();
        intent.setPackage("zhangphil.book.server");
        intent.setAction("start_zhangphil_book_server_service_action");
        bindService(intent, mServiceConnection, Service.BIND_AUTO_CREATE);
    }
}

客户端App:Client的代码组织结构:

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/83414600