AndRouter


Source link: https://github.com/campusappcn/AndRouter

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.

@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.

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.

 Router.initActivityRouter(getApplicationContext(), new IActivityRouteTableInitializer() {

 @Override

 public void initRouterTable(Map<String, Class<? extends Activity>> router) {

// only if the host is equal and pathes match, it matches.

// The url "activity://first/kris/26/birthday" is one of the matches. "kris" and 26 are values of key "name" and "age". and the "name" value type is string, the age value type is integer.

  router.put("activity://first/:s{
name
}
/:i{
age
}
/birthday", FirstActivity.class);

router.put("activity://second/:{
name
}
", SecondActivity.class);

 router.put("activity://third/home/:i{
room1
}
/printdoor/:s{
color
}
", ThirdActivity.class);

router.put("activity://four/buy/:s{
name
}
/:f{
price
}
", FourActivity.class);

 
}

}
);
 

It supports to use these two methods together.

Initialize the BrowserRouter

The same with ActivityRouter. Do this in Application class.

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 } "

 private void openSecondActivity(){

Router.open("activity://second/???");

 
}

in the SecondActivity you can get the value of name

 public void getTheValueOfName(){

String name = getIntent().getStringExtra("name")  
}

open with animation

 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

 private void openSecondActivityForResult(){

ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://second/???");

activityRoute.withOpenMethodStartForResult(this, 200)

  .open();

  
}

add extra parameters

 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

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

Usage of Browser Router

And you can use Router to open web url.

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

Add your own Router

you can define your own router and route.

private static class TestRouter extends BaseRouter{

 @Override

public void open(IRoute route) {

 Timber.i(route.getUrl());

}

 @Override

public void open(String url) {

 Timber.i(url);

}

 @Override

public IRoute getRoute(String url) {

 return new TestRoute(this, url);

}

 @Override

public boolean canOpenTheRoute(IRoute route) {

 return (route instanceof TestRoute);

}

 @Override

public boolean canOpenTheUrl(String url) {

 return TextUtils.equals(UrlUtils.getScheme(url), "test");

}

 @Override

public Class<? extends IRoute> getCanOpenRoute() {

 return TestRoute.class;

}

  
}

private static class TestRoute extends BaseRoute {

 public TestRoute(IRouter router, String url) {

 super(router, url);

}

  
}

And add it to the RouterManager

 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.

 private static final HashMap<String, String> 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:

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. 

Resources

The easiest way to use a RecyclerView on Android.

Android Version plugin is a gradle plugin that helps managing version for Android projects. Plugin is especially helpful when you are dealing with complex build structure with multiple flavours and apk splits.

A simple one class TouchListener that can be attached to any RecyclerView and handles multi selection in google photos style via long pressing on an item and moving the finger up/down to select more items (it even scrolls if you reach the edges of the RecyclerView).

Data binding based Android library for validating input.

  • Easy to use
  • Highly customisable

Android Studio / IDEA plug-ins to make Android multi-channel packages.

An Android costomized View to display battery level with charging status.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes