Teach you fast and efficient access to sdk--about application and proxies

Source: Internet
Author: User


We know that each Android application has a unique context Application object, this application generally we do not need to care about him, the system will automatically create a default application instance when the app starts. However, because application is unique throughout the application, that is, he is a single case. Therefore, some applications may wish to use application to do some work.
Fortunately, implementing a custom application is simple in Android. Directly customize a class to inherit application, and then set Android:name to the application class you customized in Androidmanifest.xml application node properties instantly.
So what's the relationship between application and U8SDK?
this is because part of the channel SDK (such as Baidu SDK), it is at the application level to do something, make access to their games, need to use their application, Or customize a application to inherit the application of the SDK and invoke their methods in the application corresponding interface.
But now the problem is, because the core idea of the U8SDK framework is to take all channels into account. It is not possible to configure the application of a channel directly in the androidmanifest.xml of the game or to customize a application to inherit the application of a channel. However, the requirements of the channel are clearly in the middle, and we must find a way to get over it. Fortunately, the method is always there. Here, I define a application listener Iapplicationlistener in the U8SDK abstraction layer, and define a u8application that inherits the application class. In the U8application class, a Iapplicationlistener instance is maintained. In this way, the corresponding interface in the Iapplicationlistener is called indirectly in the U8application Oncreate,attackbasecontext.
Thus, in the specific Access Channel SDK, we will set an adapter mode class to inherit the channel's own application, while implementing the U8SDK Abstraction Layer Iapplicationlistener interface. The corresponding method of the base class (application of the Channel SDK) is then called directly in the Iapplicationlistener interface implementation.
then, if the game layer has its own application, then you need to inherit the application u8application, if there is no custom application, The u8application is then configured directly into the Android:name attribute of the application node of the Androidmanifest.xml. (How to configure custom application can Baidu popular science a bit).
The key question now is, in the U8SDK abstraction layer of u8application, how do we know which Iapplicationlistener implementation class we need to instantiate today? In other words, if the Baidu SDK and the Xiaomi SDK I have implemented the Iapplicaitonlistener implementation class. So, how do you u8application instantiate the corresponding implementation class when generating a channel bundle? The problem is simple, we have defined a sdk_manifest.xml configuration when we access each channel. In this configuration, we add a second configuration. is to configure the implementation class for this iapplicationlistener. After the keyword of the applicationconfig node, we add another attribute: Proxyapplication. So we can configure this property like this: proxyapplication= Com.u8.sdk.BDProxyApplication.
This allows us to add the parsing of this configuration to the script of the packaging tool when merging the manifest file. The detailed mechanisms and principles of the packaging tool are described in a subsequent article. If you have purchased video to get the source in use of children's shoes. You can change the Mergemanifest method in the Package tool script apk_utils.py to the following form:

def mergemanifest (Targetmanifest, sdkmanifest): If not os.path.exists (targetmanifest) or not os.path.exists (sdkmanifes T): file_utils.printf ("The manifest file is not exists. targetmanifest:%s, sdkmanifest:%s ", Targetmanifest, Sdkmanifest) return False et.register_namespace (' Android ' , androidns) TargetTree = Et.parse (targetmanifest) TargetRoot = Targettree.getroot () et.register_namespace (' an Droid ', androidns) Sdktree = Et.parse (sdkmanifest) sdkroot = Sdktree.getroot () F = open (Targetmanifest) TA Rgetcontent = F.read () f.close () BRet = False Appconfignode = sdkroot.find (' applicationconfig ') Appnode = Targetroot.find (' application ') if appconfignode! = None and Len (appconfignode) > 0:proxyapplicationna               me = Appconfignode.get (' proxyapplication ') if proxyapplicationname! = None and Len (proxyapplicationname) > 0: Metanode = subelement (Appnode, ' meta-data ') key = ' {' + Androidns + '}name ' val = ' {' + Androidns + '}value ' Metanode.set (Key, "U8_application_prox          Y_name ") Metanode.set (Val, proxyapplicationname) Appkeyword = appconfignode.get (' keyword ') If Appkeyword! = None and Len (appkeyword) > 0:keyindex = Targetcontent.find (Appkeyword) I                         F-1 = = Keyindex:bret = True for child in List (Appconfignode): Targetroot.find (' Application '). Append (child) Permissionconfignode = Sdkroot.find (' permissionconfig ') if permis               Sionconfignode! = None and Len (permissionconfignode) > 0:for child in List (Permissionconfignode):                    Key = ' {' + Androidns + '}name ' val = child.get (key) if val! = None and Len (val) > 0:  Attrindex = Targetcontent.find (val) if-1 = = Attrindex:bret =                   True      Targetroot.append (Child) Targettree.write (targetmanifest, ' UTF-8 ') return BRet 

in this way, we implemented the Channel customization application supported in U8SDK. At the same time, the custom application of each channel will not affect the U8SDK overall framework. In U8sdk, we implement the source code of these several classes:

Package Com.u8.sdk;import Android.content.context;import android.content.res.configuration;/*** * * Define a application interface so that we can indirectly invoke the application class of the channel through that interface. * Because in the framework of U8SDK, we have no way to directly inherit or directly use the application of a channel.          * * @author Xiaohei * */public interface Iapplicationlistener {public void onproxycreate ();          public void Onproxyattachbasecontext (Context base);     public void onproxyconfigurationchanged (Configuration config); }package com.u8.sdk;import com.u8.sdk.utils.sdktools;import Android.app.application;import android.content.Context ; Import android.content.res.configuration;/** * We define our own application implementation class in the U8SDK abstraction layer. In this class, we mainly through the indirect call * Iapplicationlistener interface method to complete the actual channel application method calls. * * If the upper game, has its own application. Then you can let the application inherit u8application. * If not, you can configure u8application directly into the Android:name attribute of the application node * of the app's Androidmanifest.xml.  * * @author Xiaohei * */public class U8application extends application{private static final String Default_pkg_name =     "COM.U8.SDK";private static final String Proxy_name = "U8_application_proxy_name";          Private Iapplicationlistener listener;            public void OnCreate () {super.oncreate ();           if (listener! = null) {listener.onproxycreate ();            }} public void Attachbasecontext (Context base) {Super.attachbasecontext (base); This.            Listener = Initproxyapplication ();           if (this. Listener! = NULL) {this. Listener.onproxyattachbasecontext (base); }} public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (NEWC            Onfig);           if (this. Listener! = NULL) {this. listener.onproxyconfigurationchanged (Newconfig); }} @SuppressWarnings ("Rawtypes") Private Iapplicationlistener initproxyapplication () {String                       Proxyappname = Sdktools.getmetadata (this, proxy_name); if (ProxyappnamE = = NULL | |           Sdktools.isnullorempty (Proxyappname)) {return null; } if (Proxyappname.startswith ("."))           {proxyappname = Default_pkg_name + proxyappname;                 } try {class clazz = class. forname (Proxyappname);                           Return (Iapplicationlistener) clazz.newinstance ();           } catch (ClassNotFoundException e) {e.printstacktrace ();           } catch (Instantiationexception e) {e.printstacktrace ();           } catch (Illegalaccessexception e) {e.printstacktrace ();     } return null; }}

at the same time, in order to verify this set of things feasible, I simulated the Baidu SDK scene. Add two classes. Baiduapplication and Bdproxyapplication. Among them, Baiduapplication is the simulation of Baidu SDK own application, and Bdproxyapplication is Iapplicationlistener is the real The present class, but also inherit baiduapplication.
Package Com.u8.sdk.bd;import Android.app.application;import Android.content.context;import android.util.Log;/** * This class simulates the application class that comes with the Baidu SDK. * * @author Xiaohei * */public class Baiduapplication extends application{public void OnCreate () {Super.on           Create (); Log.                E ("Baiduapplication", "the onCreate of Baiduapplication called.");           } @Override protected void Attachbasecontext (Context base) {Super.attachbasecontext (base); Log.     E ("Baiduapplication", "the attachbasecontext of Baiduapplication called."); }}package com.u8.sdk.bd;import Com.u8.sdk.iapplicationlistener;import android.content.context;import android.content.res.configuration;/*** * * By defining a proxy class that inherits the application of the Baidu SDK while implementing U8SDK, we define the application listener interface. Thus, in the implementation of the listener method, we call the base class, which is the corresponding method of baiduapplication. * * This way, we can call the Iapplicationlistener interface to implement the corresponding method in each channel application * * @author Xiaohei */public class Bdproxyapplication extends Baiduapplication implemenTS iapplicationlistener{@Override public void Onproxycreate () {super.oncreate ();     } @Override public void Onproxyattachbasecontext (Context base) {Super.attachbasecontext (base); } @Override public void onproxyconfigurationchanged (Configuration config) {Super.onconfigurationchange     D (config); }}


This article by the small black published in this blog, reproduced please indicate the source.
More wonderful articles welcome to visit Little Black's blog: http://www.uustory.com

Teach you fast and efficient access to sdk--about application and proxies

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.