目录

1.引言

####1.1 适用对象及范围 本文档适用于策划人员、系统设计人员、开发工程师和测试工程师阅读。
####1.2 SDK作用说明 乐牛SDK为游戏开发者提供登录,支付,推送等接口。

2.SDK接入

####2.1 SDK使用准备

  • 引用乐牛SDK

将LeNiuSdk-release项目导入到工作空间中,配置游戏项目依赖该项目,如图: ![](http://wiki.leniugame.com/uploads/201701/58784701dacae_58784701.png) - **配置AndroidManifest.xml**
将以下权限,组件声明拷贝到游戏AndroidManifest.xml 中的对应位置。
也可从LeNiuSdk-release/AndroidManifest.xml文件中拷贝。
<!-- 乐牛sdk权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application>
	<!-- 乐牛sdk声明 开始 -->
	<activity
		android:name="com.leniu.official.activity.AccountRegistActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.AutoLoginActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.BindActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.FloatMenuWebViewActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.ForgetPasswordActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.IndexActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.LoginActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.MobileRegistActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.NoticeDialogActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.PyActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />
	<activity
		android:name="com.leniu.official.activity.WebViewActivity"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Translucent.NoTitleBar"
		android:windowSoftInputMode="stateHidden|adjustResize" />

	<activity
		android:name="com.leniu.activity.FloatWebViewActivity"
		android:configChanges="keyboardHidden|orientation|screenSize"
		android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

	<activity
		android:name="com.leniu.activity.SplashActivity"
		android:screenOrientation="landscape"
		android:configChanges="orientation|screenSize|navigation"
		android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
		android:windowSoftInputMode="stateHidden|adjustResize" />

	<activity
		android:name="com.leniu.activity.UpdateDialogActivity"
		android:configChanges="keyboardHidden|orientation|screenSize"
		android:theme="@android:style/Theme.Translucent.NoTitleBar" />
	<!-- 乐牛sdk声明 结束 -->

	<!-- 推送相关 -->
	<receiver android:name="com.leniu.push.receiver.PushReceiver"
		android:process=":notify" >
		<intent-filter>
			<action android:name="android.intent.action.BOOT_COMPLETED" />
			<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
			<action android:name="android.intent.action.USER_PRESENT" />
			<action android:name="com.igexin.sdk.action.refreshls" />
			<action android:name="android.intent.action.MEDIA_MOUNTED" />
			<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
			<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
			<action android:name="android.net.wifi.STATE_CHANGE"/>
			<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
			<action android:name="com.leniu.push.ACTION_TIMEUP"/>
		</intent-filter>
	</receiver>

	<receiver android:name="com.leniu.push.receiver.TimeupReceiver"
		android:process=":notify"
		android:exported="false" >
		<intent-filter>
			<action android:name="com.leniu.push.ACTION_TIMEUP"/>
			<data android:scheme="pushid"/>
		</intent-filter>
	</receiver>

	<receiver android:name="com.leniu.push.receiver.NotifyActionReceiver"
		android:process=":notify"
		android:exported="false" >
		<intent-filter>
			<action android:name="com.leniu.push.ACTION_NOTIFY"/>
			<data android:scheme="event"/>
		</intent-filter>
	</receiver>

	<service
		android:name="com.leniu.push.service.PushService"
		android:process=":notify"
		android:enabled="true" />
	<service
		android:name="com.leniu.push.service.PushService$GrayInnerService"
		android:process=":notify"
		android:enabled="true"/>
	<!--推送相关 结束-->
</application>

####2.2 接口说明 - **进程生命周期接口(必接)**
在游戏各个Activity(除闪屏Activity外,一般一个游戏只有一个Activity)生命周期中调用SDK生命周期接口。
生命周期接口说明: ```java public void onCreate(Activity activity) public void onStop(Activity activity) public void onDestroy(Activity activity) public void onResume(Activity activity) public void onPause(Activity activity) public void onStart(Activity activity) public void onRestart(Activity activity) public void onNewIntent(Intent intent) public void onActivityResult(intrequestCode, intresultCode, Intent data) ``` 用例: ```java public class MainActivity extends Activity { LeNiuSdk lnSdk = LeNiuSdk.getInstance(); public void onResume() { super.onResume(); lnSdk.onResume(this); } public void onStart() { super.onStart(); lnSdk.onStart(this); } public void onRestart() { super.onRestart(); lnSdk.onRestart(this); } public void onPause() { super.onPause(); lnSdk.onPause(this); } public void onStop() { super.onStop(); lnSdk.onStop(this); } public void onDestroy() { super.onDestroy(); lnSdk.onDestroy(this); } protected void onNewIntent(Intent intent) { super.onNewIntent(intent); lnSdk.onNewIntent(intent); } protected void onActivityResult(intrequestCode, intresultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); lnSdk.onActivityResult(requestCode, resultCode, data); } } ```
- **初始化接口(必接)**
游戏在调用登录之前,需要先调用SDK的初始化接口
初始化接口说明: ``` public void leNiuInit(Context context, AppInfo appInfo, OnInitListener initListener); ``` **参数:**
参数名 必选 类型 说明
context Context 当前上下文环境
appInfo AppInfo 应用信息,存放appid, appsecret
initListener OnInitListener 初始化结果回调

使用示例:

AppInfo info = new AppInfo();
info.setAppId("1");
info.setAppSeceret("appkey");
mLeNiuSdk.leNiuInit(MainActivity.this, info, new OnInitListener() {
		@Override
		public void onSuccess() {
			Log.i(TAG, "初始化成功");
		}
		@Override
		public void onFailure(interrorCode, String errorMsg) {
			Log.i(TAG, "初始化失败, " + errorMsg);
		}
);


- **登录接口说明(必接)**
初始化SDK成功之后,可以调用账号登录接口
public void leNiuLogin(Activity activity);

参数:

参数名 必选 类型 说明
activity Activity 当前显示游戏的Activity

- **账号登出接口说明(必接)**
需要进行账号登出,可调用账号登出接口
**登出接口说明:**
public void leNiuLogout(Activity activity);

参数:

参数名 必选 类型 说明
activity Activity 当前显示游戏的Activity

- **登录,登出结果回调监听(必接)**
当账号登录成功或者失败,或者登出账号时,通过该回调通知游戏。
游戏需要在接收到登出成功通知后,回到游戏登录界面,并重新调用登录方法 (会存在游戏没有调用leNiuLogout 方法,但是Sdk通知游戏登出账号成功的情况)。 ``` public static void setOnUserListener(OnUserListener onUserListener); ``` **参数:**
参数名 必选 类型 说明
onUserListener OnUserListener 登录,登出结果回调通知

OnUserListener 接口说明:

public static interface OnUserListener {
	//登录成功。account为登录用户名,可能不唯一。unionUid作为该用户唯一标识保存。loginToken为登录令牌,用于登录校验。
	public void onLoginSuccess(String account, String unionUid, String loginToken);
	//登录失败。
	public void onLoginFailure(int errorCode, String errorMsg);
	//账号登出成功
	public void onLogout();
}

使用示例:

//设置登录成功,失败,登出的监听回调函数
CallbackHandler.setOnUserListener(new OnUserListener() {
			@Override
			publicvoid onLoginSuccess(UserBean userBean) {
				userBean.getUnion_uid();
				Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
			}

			@Override
			publicvoid onLoginFailure(interrorCode, String errorMsg) {
				Toast.makeText(MainActivity.this, "登录失败," + errorMsg, Toast.LENGTH_SHORT).show();
			}

			@Override
			publicvoid onLogout() {
				Toast.makeText(MainActivity.this, "登出账号成功", Toast.LENGTH_SHORT).show();
			}}
);


- **用户信息扩展接口(必接)** sdk需要收集玩家角色信息,所以每次登录游戏后创建角色,角色升级,进入游戏,都需要调用用户信息扩展接口进行玩家信息收集。 ``` public void setRoleData(Context context, GameRoleBean gameRole) ``` 其中GameRoleBean 结构为:
参数名 必选 类型 说明
type String 数据类型。"1"为创建角色。"2"角色升级。"3"进入服务器。
roleid String 角色id,必须传入真实角色id
rolename String 角色名,必须传入真实角色名。
level String 角色等级,必须传入真实角色等级
server_id String 当前角色所在区服id, 必须能够转换为int 类型
server_name String 当前角色所在区服名,必须与游戏选服界面显示上的一致
roleCreateTime String 角色创建时间戳 自 Unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数,必须保存在游戏服务器,每个角色每次传入的值必须一致
balance String 虚拟金币余额
partyname String 角色所在帮派名
sword String 角色战力
mount String 角色坐骑
vip String vip等级
sex String 角色性别,可为空
ext String 预留,扩展参数

使用例子

GameRoleBean gameRole = new GameRoleBean();
gameRole.setBalance("100"); //可空
gameRole.setLevel("10");//必填
gameRole.setMount("坐骑名"); //可空
gameRole.setPartyname("帮派"); //可空
gameRole.setRoleid("12351151");//必填
gameRole.setRolename("sogood");//必填
gameRole.setServer_id("113");//服务器标识必须为数字字串
gameRole.setSex("3");//角色性别 1 男,2 女,3 未知 可空
gameRole.setSword("5"); //角色战力 可空
gameRole.setType("3");//1 创角,2 升级,3 进入游戏
gameRole.setVip("1"); //vip等级 可空
//必填,角色创建时间
gameRole.setRoleCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
//必填-区服名称
gameRole.setServer_name("限号-体验服");
JSONObject jsonObj = new JSONObject();
gameRole.setExt(jsonObj.toString()); //可空
mLeNiuSdk.setRoleData(MainActivity.this, gameRole);

- **支付接口(必接)** 在调用支付接口之前,请先确保已经调用用户信息扩展接口,否则可能造成部分渠道支付不正常的问题。 支付接口为: ``` public void leNiuCharge(Context context, String orderId, String goodName, long amount, String serverNo, String roleName, String rolelevel, boolean isFixed, String extension, OnChargeListener onChargeListener); ``` **参数:**
参数名 必选 类型 说明
context Context 当前上下文环境
orderId String 订单号
goodName String 商品名
amount long 支付金额(单位:分)
serverNo String 角色所在区服ID,必须为数字字串
roleName String 角色名
rolelevel String 角色等级
isFixed boolean 是否固定金额
extension String 透传参数
onChargeListener OnChargeListener 充值结果回调

使用示例:

mLeNiuSdk.leNiuCharge(MainActivity.this, String.valueOf(
		System.currentTimeMillis()), "商品名", 150, "serverNo", "角色名", 
		null, new OnChargeListener() {

	@Override
	publicvoid onSuccess(PayResult payResult) {
		Toast.makeText(MainActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
	}

	@Override
	publicvoid onFailure(interrorCode, String errorMsg) {
		Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
	}
});


- **退出接口(必接)**
在游戏退出前,需要调用退出接口。游戏在收到onChannelExit()方法通知后,直接退出游戏。在 收到onGameExit()方法通知后,正常执行游戏的退出逻辑。
退出接口为:
public void LeNiuExit(Context context, OnExitListener onExitListener);

参数:

参数名 必选 类型 说明
context Context 当前上下文环境
onExitListener OnExitListener 使用哪个退出方法的回调通知

使用示例:

mLeNiuSdk.LeNiuExit(this, new OnExitListener() {
	Override
	publicvoid onGameExit() {
		//执行游戏的正常退出逻辑,模拟游戏弹出退出框
		new AlertDialog.Builder(MainActivity.this)
					.setTitle("游戏界面-确认退出")
					.setMessage("确认退出?").setPositiveButton("退出",new		DialogInterface.OnClickListener() {
						@Override
						publicvoid onClick(DialogInterface dialog, intwhich) {
							finish();
						}
					}).show();
			}
			@Override
			publicvoid onChannelExit() {
			//游戏不需要弹出自己的退出游戏确认框,必须直接执行游戏退出逻辑
				finish();
			}
		});