当前位置:网站首页>Simple use example of Aidl

Simple use example of Aidl

2022-06-26 02:38:00 liulei9385

Aidl Simple use example

android-binder-demo

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)
    }
}

 Insert picture description here

原网站

版权声明
本文为[liulei9385]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180549082800.html