U8SDK技術博客:http://www.uustory.com/,歡迎來坐坐。
百度傳課已經停運,最新U8SDK視頻教程已經轉移至B站:U8SDK最新視頻教程
我們知道,每個android應用程序中都有一個唯一的上下文Application對象,這個Application一般我們不需要去關心他,應用啓動時,系統會自動創建一個默認的Application實例。但是,因爲Application在整個應用中是唯一的,也就是說,他是一個單例。所以,有的應用就可能希望利用Application來完成一些工作。
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);
}
}
更多精彩文章歡迎訪問小黑的博客:http://www.uustory.com