当前位置:网站首页>Simple use example of Aidl
Simple use example of Aidl
2022-06-26 02:38:00 【liulei9385】
Aidl Simple use example
AIDL yes Android in IPC(Inter-Process Communication) One of the ways ,AIDL yes Android Interface definition language Abbreviation , For little white ,AIDL The role is to allow you to be in your own APP There's a other one in it APP Of service, So your APP Can be with other APP Interaction .
open AndroidStudio Create two module, for example aidlClient aidlServer
1.aidl Interface file
// IBook.aidl
package com.example.androidbinddemo;
// Declare any non-default types here with import statements
interface IBook {
/** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */
void borrowBook(String number, int count);
}
2. Create a service such as BookService
class BookService : Service() {
private var handler = Handler(Looper.getMainLooper())
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
println("xxxx onStartCommand")
Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
return super.onStartCommand(intent, flags, startId)
}
override fun onBind(p0: Intent?): IBinder {
return object : IBook.Stub() {
override fun borrowBook(number: String?, count: Int) {
println("xxxx borrow number = $number count = $count")
handler.post {
Toast.makeText(
this@BookService,
"borrowBook $number $count",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
3.aidlClient How to connect BookService Operation? ?
private var serviceConnection: ServiceConnection? = null
serviceConnection = object : ServiceConnection {
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
println("xxxx onServiceConnected p0 = [${
p0}], p1 = [${
p1}]")
val iBook = IBook.Stub.asInterface(p1)
iBook?.borrowBook("you are best!",202288)
}
override fun onServiceDisconnected(p0: ComponentName?) {
println("xxxx onServiceDisconnected p0 = [${
p0}]")
}
}
val ret = bindService(
Intent("com.example.androidbinddemo.BookService").apply {
setPackage("com.example.androidbinddemo")
},
serviceConnection as ServiceConnection,
Service.BIND_AUTO_CREATE
)
println("xxxx bindService ret = $ret")
4. example
viewBinding.test.setOnClickListener {
if (iBookInterface == null) {
Toast.makeText(it.context, " Not obtained binder object ", Toast.LENGTH_SHORT).show()
} else {
iBookInterface?.borrowBook("you are best!", 202288)
}
}
边栏推荐
- Remember a simple JVM tuning experience
- 基于 ovarian 数据集 进行生存分析
- 请指教同花顺软件究竟是什么?在线开户安全么?
- The programmer's eight-year salary change has made netizens envious: you pay me one year's salary per month
- Introduction to bloc: detailed explanation of cube
- Implement decorator pattern for servicecollection
- PyQt theme
- 【系统架构】-如何评估软件架构
- Wechat applet
- Thirty ninth step
猜你喜欢
How to check and cancel subscription auto renewal on iPhone or iPad
Bloc入门之Cubit详解
[defect detection] automatic defect detection of printed circuit board based on Matlab GUI [including Matlab source code 1912]
Consumer of microservices
如何在 R 中的绘图中添加回归方程
regular expression
数据库的授权
MySQL must master 4 languages!
Cvpr2022 𞓜 future transformer with long-term action expectation
win32
随机推荐
2021-08-04
55 pictures make you feel a bit B-tree at one time
【机器学习】基于多元时间序列对高考预测分析案例
Introduction to bloc: detailed explanation of cube
Blazor University (33)表单 —— EditContext、FieldIdentifiers
程序员的八年工资变动,令网友羡慕不已:你一个月顶我一年工资
Sqlyog shortcut keys
在同花顺网上开户安全吗?如何网上开一个券商账户
[machine learning] case study of college entrance examination prediction based on multiple time series
ARM流水线如何提高代码执行效率
win32
基于邻接表的深度优先遍历
R 语言马尔可夫链蒙特卡洛:实用介绍
多测师拱墅肖sir_工作目录下的use websocket报错解决方案
官方零基础入门 Jetpack Compose 的中文课程来啦!
音视频与CPU架构
Gold three silver four~
MySQL必须掌握4种语言!
How to improve code execution efficiency with arm pipeline
Markov decision process (MDP): gambler problem