美文网首页
Android AIDL的使用

Android AIDL的使用

作者: 爱是空白日记 | 来源:发表于2021-07-19 15:40 被阅读0次

一. Android跨进程通讯的方式

借用https://blog.csdn.net/qq_41648631/article/details/103591663的图片

二. AIDL使用

首先,我们创建两个项目AIDL_SERVICE和AIDL_CLIENT用来做进程间通信

  1. 先看AIDL_SERVICE项目,在app目录点击右键新建一个AIDL文件IAIDLService.aidl


    image.png

    定义方法,供client进程调用

package com.example.aidl_service;

interface IAIDLService {
    int calculateAge(int age);
    String getName();
}

接着编译app,编译完成后会在build目录下生成了一个IAIDLService.java文件


image.png

创建一个服务,服务记得设置一个action,启动的时候会用到

package com.example.aidl_service

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log

class MyService : Service() {

    override fun onBind(p0: Intent?): IBinder? {
        return My()
    }

    class My : IAIDLService.Stub() {
        override fun calculateAge(age: Int): Int {
            return age + 100
        }

        override fun getName(): String? {
            return "张三"
        }

    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aidl_service">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService">
            <intent-filter>
                <action android:name="com.example.aidl_service.MyService" />
            </intent-filter>
        </service>
    </application>

</manifest>
  1. 再看AIDL_CLIENT这个项目
    将AIDL_SERVICE项目里aidl目录复制到AIDL_CLIENT项目的main文件夹下,编译项目生成java文件



    AIDL_CLIENT进程是通过包名和action启动MyService服务,注意这里是bindService方式。然后在onServiceConnected回调里拿到binder转成IAIDLService,之后就可以调用IAIDLService里面的方法了。

package com.example.aidl_client

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.widget.Button
import com.example.aidl_service.IAIDLService

class MainActivity : AppCompatActivity() {
    private var aidlService: IAIDLService? = null
    private lateinit var con: ServiceConnection
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        con = object : ServiceConnection {
            override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
                aidlService = IAIDLService.Stub.asInterface(p1)
            }

            override fun onServiceDisconnected(p0: ComponentName?) {
                TODO("Not yet implemented")
            }

        }
        findViewById<Button>(R.id.btn_open).setOnClickListener {
            openService()
        }
        findViewById<Button>(R.id.btn_data).setOnClickListener {
            aidlService?.run {
                Log.e("MainActivity:", "${name}${calculateAge(20)}")
            }
        }
    }

    private fun openService() {
        val intent = Intent().apply {
            `package` = "com.example.aidl_service"
            action = "com.example.aidl_service.MyService"
        }
        bindService(intent, con, BIND_AUTO_CREATE)
    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(con)
    }
}

先点击建立连接,开启Service拿到IAIDLService,然后点击获取数据就能通过IAIDLService拿到AIDL_SERVICE项目里的数据了

相关文章

网友评论

      本文标题:Android AIDL的使用

      本文链接:https://www.haomeiwen.com/subject/tymlmltx.html