Android AIDL 使用示例

介绍:

AIDL 即   Android Interface Definition Language

使用:

1.新建.aidl文件

1 //AIDL 文件所在的包
2 package com.houny.demo_aidl.aidl;
3 
4 //接口名必须和AIDL文件名一致
5 interface ISay{
6     boolean Say();
7     boolean SayInt(int i);
8     boolean SayString(String str);
9 }

2.新建Service,并在Mainfirst.xml里注册

 1 public class BackgroundService extends Service{
 2 
 3      /**
 4      * 通过这种方式实现AIDL里定义的接口
 5      */
 6      ISay.Stub say = new ISay.Stub() {
 7           
 8           //实现接口的方法
 9          // ...
10           
11      };
12      
13 
14      @Override
15      public IBinder onBind(Intent intent) {
16           //把那个接口对象返回出去
17           return say;
18      }
19 
20      //...
21 }
1  <service android:name=".service.BackgroundService"/>

3.在Activity或Fragment里绑定Service

 1     private void initAIDL() {
 2         //初始化ServiceConnection,并实现回调方法
 3         serviceConnection= new ServiceConnection() {
 4             
 5             @Override
 6             public void onServiceDisconnected(ComponentName name) {
 7                 say = null;
 8             }
 9             
10             @Override
11             public void onServiceConnected(ComponentName name, IBinder service) {
12                 //当Service绑定成功后会通过回调执行这个方法
13                 say = ISay.Stub.asInterface(service);
14             }
15         };
16     }
    private void startService() {
        Intent intent = new Intent(MainActivity.this, BackgroundService.class);
        this.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        this.startService(intent);
    }

4.使用接口定义的方法

try {
    say.SayString("Hello");
} catch (Exception e) {
    e.printStackTrace();
}

5.高级使用请参考 这个博文  @Copyright liuhe688

 

我介绍的这个基本使用示例的代码等我知道怎么上传附件的时候再上传吧

-------------------------------------------------------------------------

Change 2014-5-5 15:55:34  上传附件   点击下载

转载于:https://www.cnblogs.com/hounychang/p/3709465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 应用程序中使用 AIDL,您需要执行以下步骤: 1. 创建 AIDL 接口文件 AIDL 接口文件定义了客户端和服务端之间的通信接口。在在 Android Studio 的 app 目录下创建一个名为 aidl 的文件夹,然后在该文件夹下创建一个 .aidl 文件。例如,我们可以创建一个名为 IMyAidlInterface.aidl 的文件。 2. 定义 AIDL 接口 在 IMyAidlInterface.aidl 文件中,我们可以定义客户端和服务端之间需要交换的数据类型和方法。例如: ``` // IMyAidlInterface.aidl package com.example.myaidl; // Declare any non-default types here with import statements interface IMyAidlInterface { int add(int a, int b); } ``` 在上面的例子中,我们定义了一个名为 IMyAidlInterface 的接口,该接口包含一个 add(int a, int b) 方法,用于计算两个整数的和。 3. 实现 AIDL 接口 在服务端应用程序中,我们需要实现上述定义的 AIDL 接口。在服务端应用程序的 Java 代码中,我们需要实现 IMyAidlInterface.Stub 类,它是一个抽象类,继承自 Binder 类。在 IMyAidlInterface.Stub 类中,我们需要实现 add(int a, int b) 方法。例如: ``` public class MyAidlService extends Service { private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override public int add(int a, int b) throws RemoteException { return a + b; } }; @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } } ``` 在上面的例子中,我们实现了 add(int a, int b) 方法,该方法计算两个整数的和,并返回结果。 4. 在客户端应用程序中调用 AIDL 接口 在客户端应用程序中,我们需要绑定服务,以获取服务端的实例。然后,我们可以调用服务端的方法。例如: ``` public class MainActivity extends AppCompatActivity { private IMyAidlInterface mIMyAidlInterface; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { mIMyAidlInterface = null; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.myaidl", "com.example.myaidl.MyAidlService")); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); int result = mIMyAidlInterface.add(1, 2); Log.d("MainActivity", "Result: " + result); } } ``` 在上面的例子中,我们绑定服务,以获取服务端的实例。然后,我们调用服务端的 add(int a, int b) 方法,计算两个整数的和,并输出结果。 以上就是使用 AIDL 在不同的应用程序之间进行通信的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值