Android studio中的aidl

本文详细介绍了Android Studio中如何使用AIDL进行跨进程通信。从AIDL的基本概念出发,探讨了服务启动方式的选择,然后逐步讲解了如何创建远程服务,包括新建AIDL文件、实现远程服务以及在AndroidManifest.xml中配置服务。最后提到了本地应用的设置和测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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项目


b.选择main->New->AIDL->AIDL File创建一个AIDL文件夹






此时会在main目录下生产aidl文件


打开
IMyAidlInterface.aidl文件,可以看到


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




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



打开可以看到


其中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目录下生成和远程服务一样的东西



在本地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

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




prefect!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值