android router服务,GitHub - myjoybar/Android-Router: 一个使用简单方便的Android router框架, 一个完整的Android模块化开发示例...

Android-Router

一个使用简单方便的Android router框架,

一个完整的Android模块化开发示例

screenshot.gif

Features

支持动态注入路由

支持注解方式注入路由

支持Bundle传参

支持module之间传大容量的数据

支持添加拦截器

支持module单独作为Application编译

支持module之间同步或者异步服务调用

支持主app的Application在各个module内调用

路由引导模块:自动生成module的调用方法 ( Thank for Obo)

Installation

Gradle Dependency

Add the library to your project build.gradle

compile 'com.joybar.router:librouter:1.1.7'

compile 'com.joybar.router:compiler:1.1.7'

compile 'com.joybar.router:annotation:1.1.7'

Structure

app: 一个空壳,本身不实现任何业务逻辑,最终打包成完整的release APK

moduleshop:实现shop相关的业务逻辑,可单独编译成APK

moduleuser:实现user相关的业务逻辑,可单独编译成APK,和其它module通过router通信

librouter:router实现的核心类

screenshot1.png

Sample Usage

Step1(初始化)

方式一:动态注入路由

Router.registerRouters(new Router.RouterTable() {

@Override

public List buildRuleList() {

List ruleList = new ArrayList<>();

ruleList.add(new Rule("user", "user_main", com.joybar.moduleuser.MainActivity.class));

ruleList.add(new Rule("shop", "shop_main", com.joybar.moduleshop.MainActivity.class));

return ruleList;

}

});

方式二:注解方式注入路由

@RouterRegister(module = "user", path = "user_main")

public class MainActivity extends AppCompatActivity {

...

}

@RouterRegister(module = "shop", path = "shop_main")

public class MainActivity extends AppCompatActivity {

...

}

并在Application或者初始页面初始化注解路由器

//参数为module name

RouterInject.registerModule("shop");

RouterInject.registerModule("user");

Step2(生成路由引导模块,可选)

1. 添加注解(注解@RegisterLaunch只会读取方法参数,而launch方法本身可供模块内部调用)

@RegisterLaunch

public static void launch(Context context,String address){

Intent intent = new Intent(context,ReceiveParamActivity.class);

Bundle bundle = new Bundle();

bundle.putString("address",address);

intent.putExtras(bundle);

context.startActivity(intent);

}

@RegisterLaunch

public static void launch(Context context,String name,int id){

Intent intent = new Intent(context,ReceiveParamActivity.class);

Bundle bundle = new Bundle();

bundle.putInt("id",id);

bundle.putString("name",name);

intent.putExtras(bundle);

context.startActivity(intent);

}

2. 在此添加需要生成路由引导模块的module,运行Builder#main

public class Builder {

public static void main(String[] args) {

System.out.println("=============start build=============");

CodeMaker.autoGenerateModuleMethodName("moduleshop","/baselib/src/main/java","com.joy.baselib.guider.routertable");

System.out.println("=============end build=============");

}

}

3.在参数指定的路径下生成RouterTable$$Moduleuser和RouterTable$$Moduleshop以及相关的调用方法

RouterTable$$Moduleshop 为自动生成的类,其方法可供其他Module调用

public final class RouterTable$$Moduleshop {

public static RouterGuider launchFinishWithResult() {

// This class was generated automatically 2017-12-11 18:23:03

// module=shop,path=finish_with_result

RouterGuider routerGuider = new RouterGuider("shop", "finish_with_result");

return routerGuider;

}

public static RouterGuider launchMain() {

// This class was generated automatically 2017-12-11 18:23:03

// module=shop,path=main

RouterGuider routerGuider = new RouterGuider("shop", "main");

return routerGuider;

}

public static RouterGuider launchPostModuleData() {

// This class was generated automatically 2017-12-11 18:23:03

// module=shop,path=post_module_data

RouterGuider routerGuider = new RouterGuider("shop", "post_module_data");

return routerGuider;

}

public static RouterGuider launchReceiveParam(String name, Integer id) {

// This class was generated automatically 2017-12-11 18:23:03

// module=shop,path=receive_param

RouterGuider routerGuider = new RouterGuider("shop", "receive_param");

routerGuider.withString("name", name);

routerGuider.withInt("id", id);

return routerGuider;

}

public static RouterGuider launchReceiveParam(String address) {

// This class was generated automatically 2017-12-11 18:23:03

// module=shop,path=receive_param

RouterGuider routerGuider = new RouterGuider("shop", "receive_param");

routerGuider.withString("address", address);

return routerGuider;

}

}

Step3(使用)

1. 直接启动Activity

方式一:

Router.create()

.buildRule(new Rule("shop", "main"))

.navigate(context);

方式二 (推荐 )

RouteTable$$Moduleshop为自动生成的类

此类按照moduleuser开发者指定的规则生成

对于使用者来说,只需关注RouteTable$$moduleuser中的对外公开的方法即可

RouterTable$$Moduleshop

.launchMain()

.navigate(context);

2. startActivityForResult方式启动Activity

RouterTable$$Moduleshop.launchFinishWithResult()

.navigate(MainActivity

.this, 2);;

// OR

// Router.create()

// .buildRule(new Rule("shop", "finish_with_result"))

// .navigate(MainActivity

// .this, 2);

3. Activity之间参数传递

RouterTable$$Moduleshop

.launchReceiveParam("obo", 23)

.navigate(context);

// OR

// final Bundle bundle = new Bundle();

// bundle.putInt("id", 123);

// bundle.putString("name", "obo");

// Router.create()

// .buildRule(new Rule("shop", "receive_param"))

// .withExtra(bundle)

// .navigate(context);

4. 为Router 添加拦截器

RouterTable$$Moduleshop.launchMain()

.withRouteInterceptor(new TestInterceptor(mContext))

.withCallBack(new InterceptorCallback() {

@Override

public void onIntercept(Object result) {

Toast.makeText(mContext, result.toString(), Toast.LENGTH_LONG).show();

}

@Override

public void onContinue() {

Toast.makeText(mContext, "continue", Toast.LENGTH_LONG).show();

}

})

.navigate(mContext);

5. modlue之间同步或者异步调用服务

Step1 在module创建service类并实现IBaseService

public class ShopService implements IBaseService {

private static final String SERVICE_CMD_TEST = "cmd_test";

private static final String SERVICE_CMD_LOGIN = "cmd_login";

@Override

public Object execute(String cmd, Object... args) {

Object obj[] = args;

String msg = "";

if (obj != null && obj.length != 0) {

msg = Arrays.toString(obj);

}

if (SERVICE_CMD_TEST.equals(cmd)) {

return "成功同步调用 Shop service " + SERVICE_CMD_TEST + "服务,返回值:" + msg;

}

return null;

}

@Override

public void executeAsync(String cmd, IServiceCallBack iServiceCallBack, Object... args) {

if (SERVICE_CMD_LOGIN.equals(cmd)) {

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

if ("Tom".equals(args[0]) && "123456".equals(args[1])) {

iServiceCallBack.onSuccess("登录成功");

} else {

iServiceCallBack.onFailure(new RouterServiceException("用户名或者密码错误"));

}

}

}

@Override

public String moduleServiceName() {

return "RSShopService";

}

}

Step2 在module对应的AndroidManifest文件中注册meta信息

android:name="RSShopService"

android:value="com.joybar.moduleshop.ShopService"/>

Step3 在Application中初始化

RouterServiceManager.getInstance().init(this);// 注册组件之间服务路由

Step4 在User module中使用

//调用同步服务

IBaseService service = RouterServiceManager.getInstance().getService("RSShopService");

String result = (String) service.execute("cmd_test","ABCDE");

Toast.makeText(mContext, "User module " + result,Toast.LENGTH_LONG).show();

//调用异步服务

IBaseService service = RouterServiceManager.getInstance().getService("RSShopService");

String userName = "Tom";

String pwd = "123456";

service.executeAsync("cmd_login", new IServiceCallBack() {

@Override

public void onSuccess(Object result) {

Toast.makeText(mContext, result.toString(), Toast.LENGTH_LONG).show();

}

@Override

public void onFailure(RouterServiceException routerServiceException) {

Toast.makeText(mContext, routerServiceException.getMessage(), Toast.LENGTH_LONG).show();

}

}, userName, pwd);

6. 其它

1.单独编译module:

step1. config.gradle

isDebugType = false //主App工程

project.ext.set("is_debug_type", "false") //module 工程

step2. 执行 ./gradlew :moduleshop:assembleRelease 或者./gradlew :moduleshop:assembleDebug

2.为保证各个module能在主App的relase版本共用Application,以及每个module的application在debug和release状态下调用方法不变,请实现ApplicationService 接口

License

Copyright 2017 MyJoybar

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答您的问题。首先,您需要安装 Go 和 Beego,以及 go-socket.io 库。可以使用以下命令安装 go-socket.io: ``` go get github.com/googollee/go-socket.io ``` 接下来,您需要创建一个 Beego 项目。可以使用以下命令创建一个名为 myproject 的项目: ``` bee new myproject ``` 然后,您需要在 main.go 文件中引入 go-socket.io 库,并创建一个 Socket.IO 服务器: ```go import ( "github.com/astaxie/beego" "github.com/googollee/go-socket.io" ) func main() { // 创建 Beego 应用 app := beego.NewApp() // 创建 Socket.IO 服务器 server, err := socketio.NewServer(nil) if err != nil { beego.Error(err) } defer server.Close() // 注册事件处理函数 server.On("connection", func(so socketio.Socket) { beego.Info("on connection") // 处理客户端连接事件 }) // 将 Socket.IO 服务器挂载到 Beego 应用 app.Router("/socket.io/", &SocketIOController{server}) beego.Run() } // SocketIOController 是一个 Beego 控制器,用于处理 Socket.IO 请求 type SocketIOController struct { server *socketio.Server } func (c *SocketIOController) Get() { c.server.ServeHTTP(c.Ctx.ResponseWriter, c.Ctx.Request) } ``` 最后,您需要在前端页面中使用 Socket.IO 客户端库来连接服务器并处理事件。以下是一个简单示例: ```html <!DOCTYPE html> <html> <head> <title>Socket.IO Example</title> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect(); socket.on('connect', function() { console.log('connected'); }); socket.on('message', function(data) { console.log('message received:', data); }); socket.emit('message', 'hello'); </script> </head> <body> <h1>Socket.IO Example</h1> </body> </html> ``` 这是一个简单示例,您可以根据自己的需求进行修改和扩展。希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值