Android studio中的aidl

1.什么是AIDL?

这是百度百科的定义:
AIDL:Android Interface Definition Language,即Android接口定义语言。
Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。
我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。因此,可以将这种可以跨进程访问的服务称为AIDL(Android Interface Definition Language)服务。
总之,AIDL就是通过服务来实现跨进程通信。

2.服务。

服务作为Android中的四大组件之一,主要有两种开启方式。

start开启方式:这种方式开启的服务会常驻内存,在后台运行,例如播放音乐,网络下载等等。

bind开启方式:这种方式开启的服务和开启者不求同生,但求共死。服务会随着调用者的销毁二销毁。


思考:既然已经有了start方式,为什么还要有bind方式。

除了生命周期的差别外,bind方式可以让我们调用服务中的方法。

3.远程服务。

如果你已经熟练掌握了bind的方式调用服务中的方法,那么AIDL你也能很容易的理解。
如果你对bind方式还不太了解,那么也不会影响你对AIDL的操作。。。

a.首先建立一个远程服务,也就是新建一个AS项目
Android studio中的aidl

b.选择main->New->AIDL->AIDL File创建一个AIDL文件夹
Android studio中的aidl


Android studio中的aidl


此时会在main目录下生产aidl文件
Android studio中的aidl

打开
IMyAidlInterface.aidl文件,可以看到
Android studio中的aidl

其中basicTypes就是调用远程服务中的方法,我们可以自己定义这个方法

Android studio中的aidl


这时候选择Build-->Make Project
可以看到在build目录下回生成

Android studio中的aidl

打开可以看到
Android studio中的aidl

其中Stub继承Binder,也就是说Stub是bind服务中的那个中间人对象

c.创建远程服务

public class RemoteService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();//返回中间人对象
    }

    /**
     * 中间人的类
     */
    public class MyBinder extends IMyAidlInterface.Stub{
        @Override
        public void callRemoteMethod() throws RemoteException {
            //调用服务中的方法
            remoteMethod();
        }
    }

    Handler mHandler = new Handler();

    //定义服务中的方法
    private void remoteMethod() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "这是远程服务中的方法", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

d.在AndroidManifest中配置服务

<service android:name=".RemoteService">
    <intent-filter>
        <action android:name="com.zhouxin.remoteService"/>
    </intent-filter>
</service>


4.本地app

再创建一个as项目,用于调用远程服务中的方法

将远程服务项目中的main目录下的aidl目录复制到本地app的main目录下,然后Build-->Make Porject
在build目录下生成和远程服务一样的东西

Android studio中的aidl

在本地app中写两个按钮用于测试

<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"
    tools:context="com.zhuoxin.localapp.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bind服务"
        android:onClick="bindService"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="调用远程服务中方法"
        android:onClick="callRemoteMethod"/>

</LinearLayout>


在MAinActivity中代码如下:

public class MainActivity extends AppCompatActivity {
    private IMyAidlInterface mIMyAidlInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * 绑定服务
     * @param view
     */
    public void bindService(View view){
        Intent mIntent = new Intent();
        mIntent.setAction("com.zhouxin.remoteService");
        bindService(mIntent,new MyServiceConnection(),BIND_AUTO_CREATE);
    }

    /**
     * 调用服务中的方法
     * @param v
     */
    public void callRemoteMethod(View v){
        try {
            mIMyAidlInterface.callRemoteMethod();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    class MyServiceConnection implements ServiceConnection{



        /**
         * 服务连接成功时调用
         * @param name
         * @param service
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }

        /**
         * 服务断开时调用
         * @param name
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

4.测试结果

先运行RemoteService,在运行LocalApp

先绑定服务
在调用服务中的方法

Android studio中的aidl


prefect!!!