To start with the background, there is a need to jump to our own app from outside including other apps and the web, just like this simple need ...
To achieve this function of external jumps, we can understand how many ways the party intending to jump notifies the app to respond in a relative manner. So, if it's a jump between apps, there are a number of ways you can open an already exported=true activity directly through the package name and the specific class name, or simply use the Android broadcast notification to make the app, or open the app with a custom URI. But if the design to the Web open external applications, there is only one way, that is, the custom application of the URI to intercept, the system will automatically adjust the corresponding component response to this URI.
However, in order to do this, it is seldom just to complete external support, but also to do some internal logical jump mapping. So the need to do this is usually divided into two kinds, one is internal (application of its own jump logic), a kind of external (other applications and web jump logic).
Let us first talk about the external situation, due to uniformity, we currently only have a URI this means can be used. And here we are,
1, external jump instructions
1.1. A description of the URI.
First, we need to look at the URI, which directly refers to Https://en.wikipedia.org/wiki/URL's description. For the sake of clarification, I slightly modified the approximate format as follows:
Scheme: [//host[:p ort]] [/path] [? Query] [#fragment]
First of all, scheme is necessary, others are not necessary, but for the jump, it is obviously impossible, because you want to remove the information from this URL jump. Therefore, it is always necessary to have host and query. We often see some open source routing implementations that support so-called restful-style URLs, such as:Wytings://app/{city}/{id} , but I personally don't think it's necessary. Mainly because this kind of external jump behavior, usually less than the amount, the second should be as uniform and convenient, rather than to pursue a variety of technology cool ... The scheme that I deliberately looked at was just what I meant. This format is similar to this: weixin://qrscan?a=1&b=2
We summarize, we can do the application of the URI definition, first scheme is a must, look at the individual and company requirements, such as the next example, I define scheme for wytings, and then support the module is focused on the host field, the specific parameters are all supplemented by query. For example: wytings://user?uin=10000 Open the personal page, wytings://stockdetail?marketcode=hk&stockcode=00376 open the Stock details page and so on.
In the case of external support, usually we do not have a corresponding intent-filter limit for each activity to be supported, but instead define a common activity to intercept all external requests such as:
1<Activity2Android:name= ". Activity. Schemefilteractivity "3Android:exported= "true"4Android:theme= "@android: Style/theme.nodisplay" >5 6<intent-filter>7 8<action android:name= "Android.intent.action.VIEW"/>9 Ten<category android:name= "Android.intent.category.DEFAULT"/> One<category android:name= "Android.intent.category.BROWSABLE"/> A -<data android:scheme= "Wytings"/> - the</intent-filter> - -<intent-Filter -Android:autoverify= "true" +Tools:targetapi= "M" > -<action android:name= "Android.intent.action.VIEW"/> + A<category android:name= "Android.intent.category.DEFAULT"/> at<category android:name= "Android.intent.category.BROWSABLE"/> - -<Data -Android:host= "Native.app.wytings.com" -Android:scheme= "http"/> -<Data inAndroid:host= "Native.app.wytings.com" -Android:scheme= "https"/> to</intent-filter> +</activity>
We explain the definition of the activity:
A, android:exported this property its default is False is not open, we must be set to true, because we want to allow external access to it.
B, Android:theme= "@android: Style/theme.nodisplay" as the activity of interception, so, there is no need to show, But this nodisplay theme request must be in Onresume before finish drop activity, otherwise want to error.
C, the first Intent-filter custom scheme is wytings, which is to intercept the class URI.
D, the second scheme is HTTP, but added a special host=nativ.app.wytings.com, further detailed interception URL is: http://nativ.app.wytings.com URL. Why to intercept this URL, usually not, but in special cases, sometimes the custom scheme may be invalid, so add layer of protection, of course, also with the caller to book the URL format, such as: http://nativ.app.wytings.com/ stockdetail?marketcode=hk&stockcode=00376, because host is already defined as something else, we define the module in path and the parameters remain in query.
Then look at the implementation of Schemefilteractivity:
/*** Created by Rex on 06/10/2017. *@author[email protected]*/ Public classSchemefilteractivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Uri URI=getintent (). GetData (); LOG.I ("Wytings", "uri =" +URI); String Scheme=Uri.getscheme (); if("http". Equals (scheme) | | "HTTPS". Equals (Scheme)) {String Routemodule=uri.getlastpathsegment (); if(!Textutils.isempty (Routemodule)) {routemanager.getinstance (). Build (Routemodule+ "?" + Uri.getquery ()). Go ( This); } } Else{routemanager.getinstance (). Build (Uri.tostring ()). Go ( This); } finish (); }}
Basically interception, and then through the internal routemanager to parse processing jump. Routemanager how to deal with the implementation is too detailed, in general, the manager's responsibility is to translate the URI into a specific intent, and then initiate the corresponding activity. Interested students can take a look at all the source code of this article:
Https://github.com/wytings/AndroidRoute
2, Internal jump description
Because it's an in-app implementation, basically, how you want to implement it. However, no matter how unpredictable, can not open a core that is to establish a route mapping relationship, open the relevant page, take out the request parameters of the three steps. Let's analyze it one by one.
2.1. Establish a route mapping relationship
This is to be able to know which page a particular URL should be shown in the end. A map is usually created and then looked up.
2.3. Open the relevant page
In Android, opening a page always has its own set of logic, the system that is through the intent to start the corresponding component display.
2.4. Take out parameters
This step, or based on the intent of the system, should be intent.getxxxextra to remove the relevant parameters.
It seems to be a little difficult to look at. There is really no difficulty, simply to achieve the function. Where is the difficulty? The difficulty is that you decide to use annotations to do this ... Why use annotations? Because for which a little neat, the decoupling of the neat. The result fell into the pit ...
With annotations Theoretically, also fortunately, traverse reflection, and I personally tested a bit, the current machine really do not feel. Of course, there is no direct generation of the relevant code at compile time to come true ...
As a result, the third big challenge is to generate code at compile time, similar to Butterknife, to generate the relevant code at compile time instead of assigning values to variables at run time by reflection one by one.
Here is a thing, that is Java abstractprocessor, which is the most critical class for generating code at compile time. This has to be a second article on the Java Annotations Implementation compile-time code. Students can search the basic knowledge on the Internet, and then look at the code in this project, I also read a lot of comments on the article, but I am sorry, I did not see which is worth winning, also did not see the article worth spraying ... I do not have time to write a special article about the annotations, but can give a direction, that is to learn to debug, annotatioprocessor debugging, with different Java debugging a little difference (Google yourself), and then you can explore. In addition, I have examined, I write annotation compiler still pretty clear, you can also try to see.
Finally, again, Project address:androidroute
Run for yourself, and read the ten-year book!
About the implementation of Android routing