自定义Application
本文说明游戏自身 Application 与渠道 SDK 自定义 Application 的接入方式。
游戏母包Application配置
游戏母包
AndroidManifest.xml中application节点的android:name必须设置为com.only.sdk.OnlyApplication。
1、配置 OnlyApplication
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="com.only.sdk.OnlyApplication" >
<activity
android:name="com.only.sdk.onlysdkdemo.MainActivity"
android:label="@string/app_name"
android:screenOrientation="nosensor"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
2、如果游戏有自己的Application
如果游戏有自己的 Application,或者需要在 Application 生命周期中处理业务逻辑,可以实现 IApplicationListener 接口。
例如定义一个 GameProxyApplication:
public class GameProxyApplication implements IApplicationListener{
@Override
public void onProxyCreate() {
// 需要在 Application.onCreate 中执行的逻辑放在这里
// 如需获取原始 Application 对象,可通过 OnlySDK.getInstance().getApplication() 获取
Log.d("Game", "On Application Create");
}
@Override
public void onProxyAttachBaseContext(Context base) {
// 需要在 Application.attachBaseContext 中执行的逻辑放在这里
}
@Override
public void onProxyConfigurationChanged(Configuration config) {
// 需要在 Application.onConfigurationChanged 中执行的逻辑放在这里
}
@Override
public void onProxyTerminate() {
// 需要在 Application.onTerminate 中执行的逻辑放在这里
}
}
3、配置GameProxyApplication
在 AndroidManifest.xml 中通过 meta-data 配置上面定义的 GameProxyApplication,android:name 固定为 ONLY_GAME_APPLICATION。
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="com.only.sdk.OnlyApplication" >
<meta-data android:name="ONLY_GAME_APPLICATION" android:value="com.only.sdk.test.GameProxyApplication" />
<activity
android:name="com.only.sdk.test.MainActivity"
android:label="@string/app_name"
android:screenOrientation="nosensor"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
渠道SDK自定义Application
部分渠道 SDK 需要继承其提供的 Application,或者需要在 Application 生命周期中调用渠道接口。此时可以通过 IApplicationListener 做适配。
例如搜狗渠道 SDK 需要在 Application.onCreate 中调用 prepare 接口:
1、实现IApplicationListener接口
首先定义一个SGProxyApplication(类名自定义),实现IApplicationListener接口
public class SGProxyApplication implements IApplicationListener{
private SogouGamePlatform mSogouGamePlatform = SogouGamePlatform.getInstance();
@Override
public void onProxyCreate() {
initOnApplicationCreate(OnlySDK.getInstance().getSDKParams());
}
@Override
public void onProxyAttachBaseContext(Context base) {
// 如渠道要求,可在这里处理 attachBaseContext 逻辑
}
@Override
public void onProxyConfigurationChanged(Configuration config) {
// 如渠道要求,可在这里处理配置变化逻辑
}
@Override
public void onProxyTerminate() {
SogouGamePlatform.getInstance().onTerminate();
}
private void initOnApplicationCreate(SDKParams params){
this.gid = params.getInt("SG_GID");
this.appKey = params.getString("SG_APPKEY");
this.gameName = params.getString("SG_GAME_NAME");
this.payRatio = params.getInt("SG_PAY_RATIO");
this.coinName = params.getString("SG_COIN_NAME");
this.fixedPay = params.getBoolean("SG_FIXED_PAY");
SogouGameConfig config = new SogouGameConfig();
// 开发模式为true,false是正式环境
// 请注意,提交版本设置正式环境
config.devMode = false;
config.gid = this.gid;
config.appKey=this.appKey;
config.gameName=this.gameName;
// SDK准备初始化
mSogouGamePlatform.prepare(OnlySDK.getInstance().getApplication(), config);
}
}
2、配置ProxyApplication
实现 SGProxyApplication 后,还需要在 SDKManifest.xml 中配置 proxyApplication 属性。
配置在 applicationConfig 节点的 proxyApplication 属性上:
<applicationConfig keyword="com.sogou.gamecenter.sdk.activity.SogouLoginActivity"
proxyApplication="com.only.sdk.SGProxyApplication">
<!-- applicationConfig中其他内容,activity,service... -->
</applicationConfig>