Android 开发 AIDL 使用demo

     Android中进程通信方式有很多种,简单总结了一下,如下图所示

进程通信方式对比

 下面就不废话了,直接上代码写一个简单的demo

一.服务端

   1.先new一个AIDL file,里面写一个测试的方法:

package com.example.aidlservertest;


interface IMyAidlInterface {

   String getTestName(String s);
}

2. 新建一个Service,里面使用刚才创建的AIDL 接口代理对象赋值给 我们新创建的 Ibinder 对象,然后将 Ibinder 通过 service的onBind方法返回:

public class AIDLService extends Service {

    public final String TAG = this.getClass().getName();

    public AIDLService() {
    }

    private IBinder iBinder = new IMyAidlInterface.Stub() {
        @Override
        public String getTestName(String s) throws RemoteException {
            Log.d(TAG, "s = " + s);
            return "我是" + TAG + "返回的字符串";
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

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

3. AndroidManifest 里面注册service

 <service
            android:name=".AIDLService"
            android:enabled="true"
            android:exported="true"/>

二.接下来客户端

 1. 如果你是在两个不同的工程下去做的话,首先记得把在服务端创建的AIDL 文件 copy 到客户端,包名要一样;如果你是在一个工程下去做的话那么只用把服务端创建的service设置 android:process="com.example.remote"属性让它和客户端不在一个进程即可;

2.需要通过ServiceConnect 拿到服务端的 AIDL 代理对象,然后就可以操作里面的方法了,接着绑定服务即可,注意intent跳转里面传的service要带上包名路径:

public class MainActivity extends AppCompatActivity {

    public final String TAG = this.getClass().getName();
    private IMyAidlInterface iMyAidlInterface;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(TAG, "onServiceConnected");
                iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    String testName = iMyAidlInterface.getTestName("11111");
                    Log.d(TAG, "testName:=" + testName);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(TAG, "onServiceDisconnected");
            }
        };


    }

    public void test(View view){
        startAndBindService();
    }

    private void startAndBindService() {
        Intent service = new Intent();
        service.setClassName("com.example.aidlservertest", "com.example.aidlservertest.AIDLService");
        bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
    }
}

3.最后测试一下;

2021-03-03 18:16:38.759 17934-17934/com.example.aidlservertest D/com.example.aidlservertest.AIDLService: onCreate
2021-03-03 18:16:38.762 17934-17952/com.example.aidlservertest D/com.example.aidlservertest.AIDLService: s = 11111
2021-03-03 18:16:38.762 17988-17988/com.example.aidlclienttest D/com.example.aidlclienttest.MainActivity: testName:=我是com.example.aidlservertest.AIDLService返回的字符串

  最后,这里只是简单的写个demo记录下,其实里面还有很多东西远不止这么多,有时间可以去学习看下。

猜你喜欢

转载自blog.csdn.net/no_stop_1/article/details/114326700