阿里游戏 游戏sdk接入教程:uc接入( 四 )


至此我们就把所有UC上线所需的接口完成了,添加UI并添加相应事件调用相应接口后,就是一个最简单的demo了
如果想要了解具体的接口作用,和相应参数的说明,请继续阅读以下内容,或查看官方文档
第一种步创建监听:
SDKEventReceiver eventReceiver = new SDKEventReceiver() {@Subscribe(event = SDKEventKey.ON_INIT_SUCC)private void onInitSucc() {// 初始化成功Log.d(tag, "init success");}@Subscribe(event = SDKEventKey.ON_INIT_FAILED)private void onInitFailed(String data) {// 初始化失败Log.d(tag, "init failed:" + data);}@Subscribe(event = SDKEventKey.ON_LOGIN_SUCC)private void onLoginSucc(String sid) {// 登录成功// sid为用户的登录会话标识,用于给游戏服务器校验用户登录状态使用 。Log.d(tag, "login success:sid:" + sid);}@Subscribe(event = SDKEventKey.ON_LOGIN_FAILED)private void onLoginFailed(String desc) {// 登录失败Log.d(tag, "login failed:" + desc);}@Subscribe(event = SDKEventKey.ON_LOGOUT_SUCC)private void onLogoutSucc() {// 登出成功Log.d(tag, "logout success");}@Subscribe(event = SDKEventKey.ON_LOGOUT_FAILED)private void onLogoutFailed() {// 登出失败Log.d(tag, "logout failed");}@Subscribe(event = SDKEventKey.ON_EXIT_SUCC)private void onExit(String desc) {// 退出游戏Log.d(tag, "exit:" + desc);MainActivity.this.finish();System.exit(0);}@Subscribe(event = SDKEventKey.ON_EXIT_CANCELED)private void onExitCanceled(String desc) {// 取消退出Log.d(tag, "exit cancel:" + desc);}@Subscribe(event = SDKEventKey.ON_CREATE_ORDER_SUCC)private void onCreateOrderSucc(OrderInfo orderInfo) {// 支付成功if (orderInfo != null) {String txt = orderInfo.getOrderAmount() + ","+ orderInfo.getOrderId() + "," + orderInfo.getPayWay();Log.d(tag, "充值成功: " + txt + "\n");}Log.i(tag, "pay succ");}@Subscribe(event = SDKEventKey.ON_PAY_USER_EXIT)private void onPayUserExit(OrderInfo orderInfo) {// 支付失败if (orderInfo != null) {String txt = orderInfo.getOrderAmount() + ","+ orderInfo.getOrderId() + "," + orderInfo.getPayWay();Log.d(tag, "充值失败: " + txt + "\n");}Log.i(tag, "pay fail");}};
监听创建完成后,需要在 中注册,并在 中注销:
注册:.().ver();
@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);UCGameSdk.defaultSdk().registeSDKEventReceiver(eventReceiver);}
注销:.().();
@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();UCGameSdk.defaultSdk().unregisterSDKEventReceiver(eventReceiver);}
这样在相应操作之后,UC的SDK就能给你回调了,在相应回调方法内,做自己游戏相应操作就行
第二步初始化:
首先定义一个全局变量: sdk;
并在 中获取对象:sdk = .();
之后就在 中调用UC的初始化接口:
private void init() {// TODO Auto-generated method stubGameParamInfo gameParamInfo = new GameParamInfo();gameParamInfo.setCpId(cpId);//上文提到的游戏参数 参数类型intgameParamInfo.setGameId(gameId);//上文提到的游戏参数 参数类型intgameParamInfo.setServerId(serverId);//上文提到的游戏参数 参数类型int//设置是否支持查询充值历史和显示切换账号功能,//如果不设置,默认具有查询充值历史记录,不具有切换账户功能gameParamInfo.setEnablePayHistory(true);//开启查询充值历史功能gameParamInfo.setEnableUserChange(true);//开启账号切换功能//设置SDK屏幕方向//LANDSCAPE:横屏,横屏游戏必须设置为横屏,PORTRAIT:竖屏gameParamInfo.setOrientation(UCOrientation.PORTRAIT);SDKParams sdkParams = new SDKParams();sdkParams.put(SDKParamKey.LOG_LEVEL, UCLogLevel.DEBUG);//日志等级sdkParams.put(SDKParamKey.GAME_PARAMS, gameParamInfo);//FIXME always debug//是否打开调试模式 。//在为true的时候,会连接到联调测试环境(sdk.test4.9game.cn),//当为false的时候则会连接到正式环境(sdk.g.9game.cn) 。sdkParams.put(SDKParamKey.DEBUG_MODE, false);try {sdk.initSdk(MainActivity.this, sdkParams);} catch (AliLackActivityException e) {// TODO Auto-generated catch blocke.printStackTrace();}}