The submodule of the Android business component Sub-module and the routing router implementation between them
Source: Internet
Author: User
<span id="Label3"></p>Objective:<p><p>The previous analysis of the status of the app and some of the business components of the discussion (android business component of the status quo analysis and discussion), as well as the use of communication Bridge schema (android Business component of the URL schema use), Today's focus is on the split of the Submodule module and the routing router implementation between THEM. This article involves a lot of relevant knowledge, read this article need to understand the Java annotations (java learning annotations Annotation Implementation principle), Java dynamic Agent mechanism (java design mode proxy) and so On. Business component is a gradual process, at first it is difficult to come up with the ultimate solution, or a step-by-step approach to the more practical Reality.<em><br></em></p></p>Let's start by figuring out what a business component is.<p><p>Figuring this out is important to us, it's hard to split the business and dependency libraries, and it's hard to figure out which classes should be in the business submodule and which ones should be placed in the dependent Libraries.</p></p>1.) and Business-independent<p><p>It has nothing to do with the business, such as the various utils tools commonly used in the project, some common custom controls such as ImageView that show rounded pictures, etc.</p></p>2.) Weak business<p><p>Why this is called weak business, because these are not complete business, but also related to the app business, such as our network requests, database operations and so On.</p></p>3.) Business<p><p>This is our business component for splitting, a complete and independent line of business to be called a business, such as our app login registration Business.</p></p>Split Granularity for Business components?<p><p>The splitting of business components will be the focus of the whole rectification, the granularity of the split will also become the focus of discussion, in the end is a little bit better or finer? Coarse a little more easy to split, a little more easily decoupled flexibility, this according to the actual situation, because our project rectification process can not affect the development of new requirements, beginning or coarse a bit of granularity to split, the first roughly a few business split out, the latter slowly do Fine.</p></p>Sub-module submodule split: 1.) sub-modules are not split between<p><p>Page Jump</p></p><pre><pre><span style="color: #0000ff;">New</span> Intent (<span style="color: #0000ff;"></span>this, XXX. <span style="color: #0000ff;"> Class</span><span style="color: #000000;">); startactivity (intent);</span></pre></pre><p><p>Data transfer</p></p><p><p>Direct page Startactivityforresult return to get, indirect page through storage or variable, or through the Open-source framework Eventbus and other delivery</p></p><p><p>No split of this development method, in fact, easy to use, but this display call no task document, each jump page to communicate with the relevant developers, communication costs are Higher.</p></p>2.) after removing the molecular module<p><p>After the molecular module is disassembled, the jumps between any modules are routed router, and the following information needs to be configured in the related activity</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Activity</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Goodsdetailactivity "</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:theme</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@style/apptheme"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Intent-filter</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Data</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:host</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "goods"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:path</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "/goodsdetail"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:port</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "8888"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:scheme</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "xl"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">category</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.intent.category.DEFAULT"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Action</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.intent.action.VIEW"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">category</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:name</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "android.intent.category.BROWSABLE"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Intent-filter</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">Activity</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>Page Jump</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Specify page by URI Jump * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URL</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Openrouteruri (String url) {packagemanager packagemanager</span></span>=<span style="color: #000000;"><span style="color: #000000;">Mcontext.getpackagemanager (); Intent Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (intent.action_view, uri.parse (url)); Intent.setflags (intent.flag_activity_new_task); List</span></span><ResolveInfo> activities = Packagemanager.queryintentactivities (intent, 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>IsValid =!<span style="color: #000000;"><span style="color: #000000;">Activities.isempty (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isValid) {mcontext.startactivity (intent); } }</span></span></pre><p><p>Data transfer can also be directly page Startactivityforresult return access, indirect pages through storage or variables, or through the use of Open-source framework eventbus, etc., But these weak business common data in the Repository. There is no code reference between the Modules. In this way Android,ios and H5 clients can jump through some simple urls, maintaining a document to constrain the parameters of each Page.</p></p>3.) simple implementation of routing router<p><p>1. Define two annotation parameters, an annotated URI annotation, an indicator parameter</p></p><pre><pre>Routeruri.java</pre></pre><pre><pre><span style="color: #000000;">@Documented @target (METHOD) @Retention (RUNTIME)</span> <span style="color: #0000ff;"></span> public @<span style="color: #0000ff;">Interface</span> <span style="color: #000000;"> routeruri { </span><span style="color: #0000ff;">default</span> ""<span style="color: #000000;">;}</span></pre></pre><pre><pre>Routerparam.java</pre></pre><pre><pre><span style="color: #000000;">@Documented @target (PARAMETER) @Retention (RUNTIME)</span> <span style="color: #0000ff;"></span> public @<span style="color: #0000ff;">Interface</span> <span style="color: #000000;"> routerparam { </span><span style="color: #0000ff;">default</span> ""<span style="color: #000000;">; }</span></pre></pre><p><p>2. Define a URI protocol interface</p></p><pre><pre>Irouteruri.java</pre></pre><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">Interface</span> <span style="color: #000000;">Irouteruri { </span>= "xl://goods:8888/goodsdetail")<span style="color: #008000;">//</span> <span style="color: #008000;">request URL address</span> String Jumptogoodsdetail (@RouterParam ("goodsid") string goodsid, @RouterParam ("des") string des); <span style="color: #008000;">//</span> <span style="color: #008000;">parameter Product ID Product Description</span> <span style="color: #000000;">}</span></pre></pre><p><p>3. Define a singleton, internally through the dynamic agent mechanism to achieve the jump</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Xlrouter {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>String TAG = Xlrouter.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">. Getsimplename (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">Xlrouter minstance; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Irouteruri mrouteruri; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Context mcontext; </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Get a singleton reference * *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">Single Case</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">xlrouter getinstance (context context) {xlrouter Inst</span></span>=<span style="color: #000000;"><span style="color: #000000;">minstance; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(inst = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">synchronized</span></span>(xlrouter.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">) {inst</span></span>=<span style="color: #000000;"><span style="color: #000000;">minstance; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(inst = =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {inst</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Xlrouter (context.getapplicationcontext ()); Minstance</span></span>=<span style="color: #000000;"><span style="color: #000000;">inst; } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">inst; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Constructor Function * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Mcontext Application</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">xlrouter (Context Mcontext) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Mcontext =<span style="color: #000000;"><span style="color: #000000;">mcontext; Mrouteruri</span></span>= Create (irouteruri.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Back to API</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Irouteruri Routeruri () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mrouteruri; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Irouteruri Create (class<?><span style="color: #000000;"><span style="color: #000000;">Aclass) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>(irouteruri) Proxy.newproxyinstance (aclass.getclassloader (),<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Class<?><span style="color: #000000;"><span style="color: #000000;">[]{aclass},</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Invocationhandler () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Object Invoke (object proxy, method method, Object ... Args)<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">throwable {StringBuilder StringBuilder</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">StringBuilder (); Routeruri Requrl</span></span>= Method.getannotation (routeruri.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); LOG.E (TAG,</span></span>"ireqapi---requrl->" +<span style="color: #000000;"><span style="color: #000000;">Requrl.routeruri ()); Stringbuilder.append (requrl.routeruri ()); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">type[] parametertypes = method.getgenericparametertypes ();</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get annotation parameter types</span></span>annotation[][] Parameterannotationsarray = method.getparameterannotations ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Get parameter Annotations</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">annotation[] Annotation = method.getdeclaredannotations ();</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>pos = 0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; I < parameterannotationsarray.length; i++<span style="color: #000000;"><span style="color: #000000;">) {annotation[] Annotations</span></span>=<span style="color: #000000;"><span style="color: #000000;">parameterannotationsarray[i]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(annotations! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&& annotations.length! = 0<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(pos = = 0<span style="color: #000000;"><span style="color: #000000;">) {stringbuilder.append (</span></span>"?"<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{stringbuilder.append (</span></span>"&"<span style="color: #000000;"><span style="color: #000000;">); } POS</span></span>++<span style="color: #000000;"><span style="color: #000000;">; Routerparam Reqparam</span></span>= (routerparam) Annotations[0<span style="color: #000000;"><span style="color: #000000;">]; Stringbuilder.append (reqparam.value ()); Stringbuilder.append (</span></span>"="<span style="color: #000000;"><span style="color: #000000;">); Stringbuilder.append (args[i]); LOG.E (TAG,</span></span>"reqparam---reqparam->" + reqparam.value () + "=" +<span style="color: #000000;"><span style="color: #000000;">args[i]); } } </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Below you can perform the corresponding jump operation</span></span><span style="color: #000000;"><span style="color: #000000;">Openrouteruri (stringbuilder.tostring ()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } }); } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Specify page by URI Jump * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">URL</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Openrouteruri (String url) {packagemanager packagemanager</span></span>=<span style="color: #000000;"><span style="color: #000000;">Mcontext.getpackagemanager (); Intent Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Intent (intent.action_view, uri.parse (url)); Intent.setflags (intent.flag_activity_new_task); List</span></span><ResolveInfo> activities = Packagemanager.queryintentactivities (intent, 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>IsValid =!<span style="color: #000000;"><span style="color: #000000;">Activities.isempty (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(isValid) {mcontext.startactivity (intent); } }}</span></span></pre><p><p>4. Invocation Mode</p></p><pre><pre> Xlrouter.getinstance (context). Routeruri (). jumptogoodsdetail ("1000110002", "goods des");</pre></pre>Summarize:<p><p>To achieve real business component weight and far, we implement the first step to remove the molecular module, let the code of each module maintenance, first decoupling their dependencies, in the app Shell project through compile project (': umeng_social_sdk_library ') This way selectively access to which sub-module, the development process to develop which module to introduce which module, testing is the same, testing which module to introduce which module, the final test needs to introduce all the relevant modules, test ON-LINE.</p></p><p><p></p></p><p><p>The submodule of the Android business component Sub-module and the routing router implementation between them</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service