Android AIDL示例

AIDL进程间通信简介项目结构服务端客户端github地址简介aidl:android interface definition language(安卓接口定义语言),主要用于安卓系统中的跨进程通信。项目结构aidlserver:服务端,包含aidl、service、序列化的实体类。 aidlclient:客户端,包含aidl、实体类、绑定service代码。服务端1. 创建aidl文
摘要由CSDN通过智能技术生成

AIDL进程间通信

  • 简介
  • 项目结构
  • 服务端
  • 客户端
  • github地址

简介

aidl:android interface definition language(安卓接口定义语言),主要用于安卓系统中的跨进程通信。

项目结构

aidlserver:服务端,包含aidl、service、序列化的实体类。
aidlclient:客户端,包含aidl、实体类、绑定service代码。

服务端

1. 创建aidl文件。
在android studio创建一个服务端(注:本博客中所有所说的服务端和客户端都是相对的,因为安卓系统中的每一个项目都可以充当服务端和客户端,只是为了说明aidl才这么称呼它们)项目,本例中项目名称为aidlserver。
首先创建aidl文件:在项目名称右键—>New—>AIDL—>AIDL File,Android Studio可以自动创建aidl目录和文件。
2. 定义ICalculator接口。
写一个方法int add(int a, int b),该方法计算两个参数的和作为返回值,写完该aidl后点击Sync按钮同步一下工程,可以自动生成ICalculator.java文件。


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Android AIDL 示例代码: 首先,需要定义一个 AIDL 接口文件,例如 MyService.aidl: ``` package com.example.myservice; interface MyService { String getMessage(); } ``` 然后在服务端实现这个接口,例如 MyService.java: ``` package com.example.myservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { private final MyServiceImpl mImpl = new MyServiceImpl(); private static class MyServiceImpl extends IMyService.Stub { @Override public String getMessage() { return "Hello from MyService!"; } } @Override public IBinder onBind(Intent intent) { return mImpl; } } ``` 最后,在客户端绑定服务并调用接口方法,例如 MainActivity.java: ``` package com.example.myapplication; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.example.myservice.MyService; public class MainActivity extends AppCompatActivity { private MyService mService; private boolean mBound = false; private final ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = MyService.Stub.asInterface(service); mBound = true; // 调用 MyService 中定义的接口方法 try { String message = mService.getMessage(); TextView textView = findViewById(R.id.text_view); textView.setText(message); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { mService = null; mBound = false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 绑定 MyService Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.myservice", "com.example.myservice.MyService")); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); if (mBound) { unbindService(mConnection); mBound = false; } } } ``` 鉴于这只是一个简单的示例,实际的应用场景可能会更加复杂。需要注意的是,AIDL 接口中定义的方法的参数和返回值必须是支持跨进程传输的类型,例如基本类型、String、Parcelable 等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值