android 打电话模块,Android 模块间通信

注意事项:

1 ARouter的引用在Java和kotlin中不同

Kotlin (推荐)

1 在basemodule中添加引用

api 'com.alibaba:arouter-api:1.5.1'

kapt 'com.alibaba:arouter-compiler:1.5.1'

2 「每个module」中添加下面代码(注意)

apply plugin: 'kotlin-kapt'

javaCompileOptions {

annotationProcessorOptions {

arguments = [AROUTER_MODULE_NAME: project.getName()]

}

}

kapt 'com.alibaba:arouter-compiler:1.5.1'

JAVA

1 在basemodule中添加引用

api 'com.alibaba:arouter-api:1.5.1'

annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'

2 每个module中添加下面代码(注意)

javaCompileOptions {

annotationProcessorOptions {

arguments = [AROUTER_MODULE_NAME: project.getName()]

}

}

annotationProcessor 'com.alibaba:arouter-compiler:1.5.1'

示例代码:

c8ad49b53462

image.png

步骤:

1 base目录下创建IBaseService接口

2 创建IBaseService实现类,BaseService

3 被调用方receiver,定义路由TestService

4 调用方caller,获取service对象,获取数据

base module

1 IBaseService

import com.alibaba.android.arouter.facade.template.IProvider

interface IBaseService : IProvider {

fun getData(): T?

fun setData(t: T?)

}

2 BaseService

import android.content.Context

open class BaseService : IBaseService {

var t: T? = null

override fun getData(): T? {

return t

}

override fun init(context: Context?) {

}

override fun setData(t: T?) {

this.t = t

}

}

2 ProviderHelper (获取service工具类)

import com.alibaba.android.arouter.launcher.ARouter

class ProviderHelper {

companion object {

fun getService(): BaseService {

return ARouter.getInstance().build("/test/receiver").navigation() as BaseService

}

}

}

receiver module

1 TestService

import com.alibaba.android.arouter.facade.annotation.Route

import com.example.base.BaseService

@Route(path = "/test/receiver")

class TestService : BaseService() {

override fun getData(): String? {

setData("sssss")

return super.getData()

}

}

caller module调用

var service = ProviderHelper.getService()

var msg = service.getData()

Toast.makeText(this@CallerActivity, msg, Toast.LENGTH_SHORT).show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值