标签:u8sdk sdk接入 sdk接入教程 anysdk原理 棱镜sdk原理
def mergeManifest(targetManifest, sdkManifest): if not os.path.exists(targetManifest) or not os.path.exists(sdkManifest): 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('android', androidNS) sdkTree = ET.parse(sdkManifest) sdkRoot = sdkTree.getroot() f = open(targetManifest) targetContent = f.read() f.close() bRet = False appConfigNode = sdkRoot.find('applicationConfig') appNode = targetRoot.find('application') if appConfigNode != None and len(appConfigNode) > 0: proxyApplicationName = 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_PROXY_NAME") metaNode.set(val, proxyApplicationName) appKeyWord = appConfigNode.get('keyword') if appKeyWord != None and len(appKeyWord) > 0: keyIndex = targetContent.find(appKeyWord) if -1 == keyIndex: bRet = True for child in list(appConfigNode): targetRoot.find('application').append(child) permissionConfigNode = sdkRoot.find('permissionConfig') if permissionConfigNode != 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
package com.u8.sdk; import android.content.Context; import android.content.res.Configuration; /*** * * 定义一个Application接口,这样我们就可以通过该接口去间接调用渠道的Application类。 * 因为在u8sdk这套框架中,我们没有办法直接继承或者直接使用某个渠道的Application。 * * @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; /** * 我们在u8sdk抽象层中,定义一个我们自己的Application实现类。在这个类中,我们主要是通过间接的调用 * IApplicationListener接口的方法来完成实际各个渠道Application中方法的调用。 * * 如果上层游戏,有自己的Application。那么可以让该Application继承U8Application即可。 * 如果没有,则可以直接将U8Application配置到应用的AndroidManifest.xml的application节点 * 的android:name属性中。 * * @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(newConfig); 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; } }
package com.u8.sdk.bd; import android.app.Application; import android.content.Context; import android.util.Log; /** * 这个类 模拟百度SDK里面自带的Application类。 * * @author xiaohei * */ public class BaiduApplication extends Application{ public void onCreate(){ super.onCreate(); 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; /*** * * 通过定义一个代理类,继承百度SDK的Application,同时实现u8sdk里,我们定义的Application监听器接口。这样,在监听器方法的实现中 * 我们调用基类也就是BaiduApplication的相应方法。 * * 这样后面,我们就可以通过调用IApplicationListener接口,实现各个渠道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.onConfigurationChanged(config); } }
教你快速高效接入SDK——关于Application的适配和代理
标签:u8sdk sdk接入 sdk接入教程 anysdk原理 棱镜sdk原理
原文地址:http://blog.csdn.net/chenjie19891104/article/details/43667007