当前位置:网站首页>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)
}
}

边栏推荐
猜你喜欢

表达式的动态解析和计算,Flee用起来真香

Implementation of depth first traversal based on adjacency matrix

win32

Wechat launched a web version transmission assistant. Is it really easy to use?

Eureka registration information configuration memo

@Query difficult and miscellaneous diseases

组件与路由

【图像过滤】基于matlab GUI图像过滤系统【含Matlab源码 1913期】

业务流程图设计

数字商品DGE--数字经济的财富黑马
随机推荐
Markov decision process (MDP): Blackjack problem (mc-es)
Sloppy formula
Share some remote office experience in Intranet operation | community essay solicitation
第一章:渗透测试的本质信息收集
df报错Stale file handle
图的深度优先遍历
饼图变形记,肝了3000字,收藏就是学会!
深度好文:什么是超网 Supernetting?
Tab switch
Oracle练习
leetcode 300. Longest Increasing Subsequence 最长递增子序列 (中等)
[defect detection] automatic defect detection of printed circuit board based on Matlab GUI [including Matlab source code 1912]
ROS2+DDS+RTPS
Sqlyog shortcut keys
Back to top case
如何在 R 中的绘图中添加回归方程
为 ServiceCollection 实现装饰器模式
Magnifier case
IPhone 13 screen stuck black, unable to shut down? How to solve
多测师拱墅肖sir_工作目录下的use websocket报错解决方案