百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

AndRouter

> 编程语言
开源

一个用于将 URL 映射到活动或操作的 Android 路由器框架。

705 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用于将 URL 映射到活动或操作的 Android 路由器框架。

AndRouter

AndRouter is an Android framework used to map urls to activities or actions.

中文设计文档

Android路由框架设计与实现

Features

  • AndRouter implements activities router, so it only needs to define the activities mapping table and enjoy using url to open activities.
  • AndRouter implements Browser router, so it can be used to open a web url. It will use the system browser to open it.
  • AndRouter supports adding a user-defined router.
  • RouteManager will use the first router that can open the url in the list to open it. So the router added earlier will have higher priority.
  • The default scheme of activities router is "activity", and browser router is "http" and "https". It's allowed to change them.
  • It supports to change the behavior of the default activity router and browser router.
  • It can only add one object of a router class to the Router, or it will delete the router object that has the same class that you added and add the new one.

Usage

Initialize

It needs to be initialized if you want to use the default activity router and browser router.

Initialize the ActivityRouter

Here are two methods to initialize the ActivityRouter.

Annotation

It supports using Java annotations to map urls to Activities. For example, the code below maps the urls of "activity://second" and "activity://second2" to SecondActivity.

java
@RouterMap({"activity://second", "activity://second2"})
public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

And in the Application class of your app, you need to init the ActivityRouter.

java
public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Router.initActivityRouter(getApplicationContext());
    }
}

Initializer class

Or you can use an IActivityRouteTableInitializer implementation to add the activity router table. In the code below, some routes are added. For example, the route "activity://first/:s{name}/:i{age}/birthday" will map to FirstActivity.class. The route consists of four parts. It's recommended to do this in the Application class of your app.

…

It supports to use these two methods together.

Initialize the BrowserRouter

The same with ActivityRouter. Do this in Application class.

java
public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Router.initBrowserRouter(getApplicationContext());
    }
}

Definition Of Route Url

  • Scheme: Normally, define which router to use, the "activity" in the example.
  • Host: Normally, define where to go, the "first" in the example.
  • Path: Define key value and path, the "/:s{name}/:i{age}/birthday" in the example. The path segments can be divided into two types. One, the fixed path, such as "birthday", it's fixed. And the value key such as the :s{name}, it defines values in the path. In url, it will be replaced with value. For example, ":s{name}" can be replaced with "kris", and it will be set to the intent extras with putExtra("name", "kris"). Then in the routing activity, the value of "name" can be get with getStringExtra("name"). The table below showes the value key format. ":{}" is essential. And the character after ':' defines the type of the value. If the url mathes but the value type not matches, it will throw RuntimeException, which you should pay attention to.
key format :i{key} :f{key} :l{key} :d{key} :s{key} or :{key} :c{key}
type integer float long double string char
  • Query parameters: It supports adding some option parameters in the Query parameters. But it will not influence the route match. For example, the url "activity://first/kris/26?wifename=marry" matches to "activity://first/:{name}/:i{age}". Then in the FirstActivity, the value of "wifename" can be got.

Definition of match

If a url matches a route, the url's scheme and host are equal to the route. And the paths except the value key are equal.

Match Examples

If the route is "activity://main/:{name}/feed/:i{page}/list".

Matches:
  • activity://main/kris/feed/12/list
  • activity://main/bob/feed/13/list
  • activity://main/jim/feed/14/list?q=8
Not Matches
  • hello://main/kris/feed/12/list | scheme not equal
  • activity://second/kris/feed/12/list | host not equal
  • activity://main/kris/fed/12/list | path not match
  • activity://main/kris/12/list | path not match

Useage of Activity Router

open with startActivity(), it matches the route as "activity://second/:{name}"

java
	
 	private void openSecondActivity(){
        Router.open("activity://second/汤二狗");
   }

in the SecondActivity you can get the value of name

java
	public void getTheValueOfName(){
		String name = getIntent().getStringExtra("name")
	}

open with animation

java
    private void openSecondActivityWithVerticalAnim(){
        ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://second/汤二狗");
        activityRoute
                .setAnimation(this, R.anim.in_from_left, R.anim.out_to_right)
                .open();
    }

open for result

java
    private void openSecondActivityForResult(){
        ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://second/汤二狗");
        activityRoute.withOpenMethodStartForResult(this, 200)
                .open();
    }

add extra parameters

java
    private void openSecondActivityWithExtraValue(){
        Date date = new Date();
        ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://third");
        activityRoute
                .withParams("date", date)
                .open();
    }

you can get the extra parameters in the ThirdActivity

java
	Date date = (Date) getIntent().getSerializableExtra("date");

Usage of Browser Router

And you can use Router to open web url.

java
	Router.open("http://www.baidu.com");

Add your own Router

you can define your own router and route.

…

And add it to the RouterManager

java
	Router.addRouter(new TestRouter());

Interceptor

It support interceptor after version 1.2.8. There is a example that intercept url in black list and go to another error page. You can also use it to do the things like going to the login page when users want to do something needing to be logined.

java
	private static final HashMap INTERCEPTOR_BLACK_LIST_SET = new LinkedHashMap<>();
	static {
   		INTERCEPTOR_BLACK_LIST_SET.put("http://www.souhu.com", "activity://error");
   		INTERCEPTOR_BLACK_LIST_SET.put("activity://intercepted", "activity://error");
   	}
	Router.setInterceptor(new Interceptor() {
	
	  	@Override
	   public boolean intercept(Context context, String url) {
	   		if(INTERCEPTOR_BLACK_LIST_SET.keySet().contains(url)){
	   			Router.open(context, INTERCEPTOR_BLACK_LIST_SET.get(url));
	    	      return true;
	       }
	    	return false;
	   }
   });  

Install

In the build.gradle file of your project. You need to:

Add dependeny to apt:

java
buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

Add jitpack repository:

allprojects {
 repositories {
    jcenter()
    maven { url "https://jitpack.io" }
 }
}

Then, in the build.gradle file of your app module, you need to add these dependencies.

apply plugin: 'android-apt'

dependencies {
    compile 'com.github.campusappcn.AndRouter:router:1.2.8'
    apt 'com.github.campusappcn.AndRouter:compiler:1.2.8'
}

Note: do not add the jitpack.io repository under buildscript

Feedback

If you have any problems, welcome, and please share any issues.

License

Copyright 2015 杭州树洞网络科技有限公司

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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaandroidrouter

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言