组件化搭建的过程中需要模块间的通信功能,一般常用的是阿里的Arouter、CC等第三方库通信,最近了解google的开源库AutoService,所以简单写一篇Demo

导包

首选base的module中引入库

    api 'com.google.auto.service:auto-service-annotations:1.0-rc6'

然后只需要在被跳转的Activity/Fragment中

    annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6'

如果是kotlin则需要在module中的build中添加

apply plugin: 'kotlin-kapt'

然后再用kapt导入

 kapt 'com.google.auto.service:auto-service:1.0-rc6'

使用

首先我们新建一个module模块autodemo,然后新建一个需要打开的DemoActivity

package com.example.autodemo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.autodemo.databinding.ActivityDemoBinding;
import com.example.autodemo.utils.Constants;

import java.util.Objects;


public class DemoActivity extends AppCompatActivity {
    private ActivityDemoBinding mBinding;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_demo);
        mBinding.title.setText(getIntent().getStringExtra(Constants.TITLE));
        mBinding.webview.getSettings().setJavaScriptEnabled(true);
        mBinding.webview.loadUrl(Objects.requireNonNull(getIntent().getStringExtra(Constants.URL)));
        mBinding.actionBar.setVisibility(getIntent().getBooleanExtra(Constants.IS_SHOW_ACTION_BAR, true) ? View.VISIBLE : View.GONE);
        mBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoActivity.this.finish();
            }
        });
    }
}

然后再common或者base的module中定义一个接口


```java
public interface IDemoService {
    void startDemoActivity(Context context, String url, String title, boolean isShowActionBar);
}

然后在你的autodemo模块中实现这个接口

package com.example.autodemo;

import android.content.Context;
import android.content.Intent;

import com.example.autodemo.utils.Constants;
import com.google.auto.service.AutoService;
import com.example.common.autoservice.IDemoService;

@AutoService({IDemoService.class})
public class DemoServiceImpl implements IDemoService {
    @Override
    public void startDemoActivity(Context context, String url, String title, boolean isShowActionBar) {
        if (context != null) {
            Intent intent = new Intent(context, DemoActivity.class);
            intent.putExtra(Constants.TITLE, title);
            intent.putExtra(Constants.URL, url);
            intent.putExtra(Constants.IS_SHOW_ACTION_BAR, isShowActionBar);
            context.startActivity(intent);
        }
    }
}

再接口中实现startActivity 启动你要启动的DemoActivity 同事使用@AutoService注解 注解你所实现的接口名称

最后在你需要启动这个Activity时候

   IDemoService demoService = AutoServiceLoader.load(IDemoService.class);
                if(demoService != null) {
                    demoService.startDemoActivity(MainActivity.this, "https://www.baidu.com", "百度", false);
                }

这里我自己抽取的一个公共类AutoServiceLoader

public final class AutoServiceLoader {
    private AutoServiceLoader() {
    }

    public static <S> S load(Class<S> service) {
        try {
            return ServiceLoader.load(service).iterator().next();
        } catch (Exception e) {
            return null;
        }
    }
}

这实现了简单的启动

总结

具体实现源码 回头在学习 哈哈哈

源码地址

https://download.csdn.net/download/ZhaDanRen_/12701902
白嫖渠道
https://github.com/W1913/MvvmDemo

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐