package com.mygame.test;
|
|
import java.util.UUID;
|
|
import android.Manifest;
|
import android.app.Activity;
|
import android.app.AlertDialog;
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.pm.PackageManager;
|
import android.os.Bundle;
|
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.content.ContextCompat;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.View.OnClickListener;
|
import android.widget.Button;
|
import android.widget.TextView;
|
|
import com.quicksdk.QuickSDK;
|
import com.quicksdk.Sdk;
|
import com.quicksdk.entity.GameRoleInfo;
|
import com.quicksdk.entity.OrderInfo;
|
import com.quicksdk.entity.UserInfo;
|
import com.quicksdk.notifier.ExitNotifier;
|
import com.quicksdk.notifier.InitNotifier;
|
import com.quicksdk.notifier.LoginNotifier;
|
import com.quicksdk.notifier.LogoutNotifier;
|
import com.quicksdk.notifier.PayNotifier;
|
import com.quicksdk.notifier.SwitchAccountNotifier;
|
|
|
public class MainActivity extends Activity implements OnClickListener {
|
|
private Button btnLogin, btnPay, btnLogout, btnFinish, btnUloadUserInfo;
|
private TextView infoTv;
|
|
private Context mContext;
|
boolean isLandscape = false;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
mContext = getApplicationContext();
|
setContentView(getResId("activity_main", "layout"));
|
|
initView();
|
|
// 设置横竖屏,游戏横屏为true,游戏竖屏为false(必接)
|
QuickSDK.getInstance().setIsLandScape(isLandscape);
|
|
// 生命周期接口调用(必接)
|
|
// 当targetVer大于23时 需要动态申请读写等权限 具体权限 具体而定
|
try {
|
// check权限
|
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
|
|| (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
|
// 没有 , 申请权限 权限数组
|
ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_PHONE_STATE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
|
} else {
|
// 有 则执行初始化
|
// 设置通知,用于监听初始化,登录,注销,支付及退出功能的返回值(必接)
|
initQkNotifiers();
|
// 请将下面语句中的第二与第三个参数,替换成QuickSDK后台申请的productCode和productKey值,目前的值仅作为示例
|
Sdk.getInstance().init(this, "88049844578484520615487574815873", "82414864");
|
}
|
} catch (Exception e) {
|
// 异常 继续申请
|
ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_PHONE_STATE,
|
Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
|
}
|
com.quicksdk.Sdk.getInstance().onCreate(this);
|
}
|
//申请权限的回调(结果)
|
@Override
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
//申请成功
|
initQkNotifiers();
|
Sdk.getInstance().init(this, "88049844578484520615487574815873", "82414864");
|
} else {
|
//失败 这里逻辑以游戏为准 这里只是模拟申请失败 退出游戏 cp方可改为继续申请 或者其他逻辑
|
System.exit(0);
|
finish();
|
}
|
}
|
@Override
|
protected void onStart() {
|
super.onStart();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onStart(this);
|
}
|
|
@Override
|
protected void onRestart() {
|
super.onRestart();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onRestart(this);
|
}
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onPause(this);
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onResume(this);
|
}
|
|
@Override
|
protected void onStop() {
|
super.onStop();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onStop(this);
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onDestroy(this);
|
}
|
|
@Override
|
protected void onNewIntent(Intent intent) {
|
super.onNewIntent(intent);
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onNewIntent(intent);
|
}
|
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
// 生命周期接口调用(必接)
|
com.quicksdk.Sdk.getInstance().onActivityResult(this, requestCode, resultCode, data);
|
}
|
|
@Override
|
public void onClick(View v) {
|
int id = v.getId();
|
// 登录
|
if (id == getResId("btn_login", "id")) {
|
Log.d("quicksdk",""+com.quicksdk.Extend.getInstance().getDeviceID(MainActivity.this));
|
com.quicksdk.User.getInstance().login(MainActivity.this);
|
}
|
|
// 提交角色信息
|
if (id == getResId("btn_uploadUserInfo", "id")) {
|
setUserInfo();
|
}
|
|
// 注销
|
if (id == getResId("btn_logout", "id")) {
|
com.quicksdk.User.getInstance().logout(MainActivity.this);
|
}
|
|
// 支付
|
if (id == getResId("btn_pay", "id")) {
|
pay();
|
}
|
|
// 退出
|
if (id == getResId("btn_finish", "id")) {
|
exit();
|
}
|
}
|
|
/**
|
* 支付
|
*/
|
private void pay() {
|
GameRoleInfo roleInfo = new GameRoleInfo();
|
roleInfo.setServerID("1");// 服务器ID,其值必须为数字字符串
|
roleInfo.setServerName("火星服务器");// 服务器名称
|
roleInfo.setGameRoleName("裁决之剑");// 角色名称
|
roleInfo.setGameRoleID("1121121");// 角色ID
|
roleInfo.setGameUserLevel("12");// 等级
|
roleInfo.setVipLevel("Vip4");// VIP等级
|
roleInfo.setGameBalance("5000");// 角色现有金额
|
roleInfo.setPartyName("");// 公会名字
|
|
OrderInfo orderInfo = new OrderInfo();
|
orderInfo.setCpOrderID(UUID.randomUUID().toString().replace("-", ""));// 游戏订单号
|
orderInfo.setGoodsName("元宝");// 产品名称
|
// orderInfo.setGoodsName("月卡");
|
orderInfo.setCount(10);// 购买数量,如购买"10元宝"则传10
|
// orderInfo.setCount(1);// 购买数量,如购买"月卡"则传1
|
orderInfo.setAmount(10); // 总金额(单位为元)
|
orderInfo.setGoodsID("101"); // 产品ID,用来识别购买的产品
|
orderInfo.setExtrasParams("透传参数"); // 透传参数
|
|
com.quicksdk.Payment.getInstance().pay(MainActivity.this, orderInfo, roleInfo);
|
}
|
|
/**
|
* 退出
|
*/
|
private void exit() {
|
// 先判断渠道是否有退出框,如果有则直接调用quick的exit接口
|
if (QuickSDK.getInstance().isShowExitDialog()) {
|
Sdk.getInstance().exit(MainActivity.this);
|
} else {
|
// 游戏调用自身的退出对话框,点击确定后,调用quick的exit接口
|
new AlertDialog.Builder(MainActivity.this).setTitle("退出").setMessage("是否退出游戏?").setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface arg0, int arg1) {
|
Sdk.getInstance().exit(MainActivity.this);
|
}
|
}).setNegativeButton("取消", null).show();
|
}
|
}
|
|
/**
|
* 向渠道提交用户信息。 在创建游戏角色、进入游戏和角色升级3个地方调用此接口,当创建角色时最后一个参数值为true,其他两种情况为false。
|
* GameRoleInfo所有字段均不能传null,游戏没有的字段请传一个默认值或空字符串。
|
*/
|
public void setUserInfo() {
|
GameRoleInfo roleInfo = new GameRoleInfo();
|
roleInfo.setServerID("1");// 服务器ID
|
roleInfo.setServerName("火星服务器");// 服务器名称
|
roleInfo.setGameRoleName("裁决之剑");// 角色名称
|
roleInfo.setGameRoleID("1121121");// 角色ID
|
roleInfo.setGameUserLevel("12");// 等级
|
roleInfo.setVipLevel("9"); // 设置当前用户vip等级,必须为整型字符串
|
roleInfo.setGameBalance("5000"); // 角色现有金额
|
roleInfo.setGameUserLevel("12"); // 设置游戏角色等级
|
roleInfo.setPartyName("无敌联盟"); // 设置帮派,公会名称
|
roleInfo.setRoleCreateTime("1473141432"); // UC与1881渠道必传,值为10位数时间戳
|
roleInfo.setPartyId("1100"); // 360渠道参数,设置帮派id,必须为整型字符串
|
roleInfo.setGameRoleGender("男"); // 360渠道参数
|
roleInfo.setGameRolePower("38"); // 360渠道参数,设置角色战力,必须为整型字符串
|
roleInfo.setPartyRoleId("11"); // 360渠道参数,设置角色在帮派中的id
|
roleInfo.setPartyRoleName("帮主"); // 360渠道参数,设置角色在帮派中的名称
|
roleInfo.setProfessionId("38"); // 360渠道参数,设置角色职业id,必须为整型字符串
|
roleInfo.setProfession("法师"); // 360渠道参数,设置角色职业名称
|
roleInfo.setFriendlist("无"); // 360渠道参数,设置好友关系列表,格式请参考:http://open.quicksdk.net/help/detail/aid/190
|
com.quicksdk.User.getInstance().setGameRoleInfo(this, roleInfo, true);
|
}
|
|
/**
|
* 初始化界面元素变量及设置点击监听
|
*/
|
private void initView() {
|
btnLogin = (Button) findViewById(getResId("btn_login", "id"));
|
btnLogin.setOnClickListener(this);
|
|
btnPay = (Button) findViewById(getResId("btn_pay", "id"));
|
btnPay.setOnClickListener(this);
|
|
btnLogout = (Button) findViewById(getResId("btn_logout", "id"));
|
btnLogout.setOnClickListener(this);
|
|
btnFinish = (Button) findViewById(getResId("btn_finish", "id"));
|
btnFinish.setOnClickListener(this);
|
|
btnUloadUserInfo = (Button) findViewById(getResId("btn_uploadUserInfo", "id"));
|
btnUloadUserInfo.setOnClickListener(this);
|
|
infoTv = (TextView) findViewById(getResId("tv_userInfo", "id"));
|
}
|
|
/**
|
* 返回资源id
|
*/
|
public int getResId(String name, String defType) {
|
return mContext.getResources().getIdentifier(name, defType, mContext.getPackageName());
|
}
|
|
/**
|
* 设置通知,用于监听初始化,登录,注销,支付及退出功能的返回值
|
*/
|
private void initQkNotifiers() {
|
QuickSDK.getInstance()
|
// 1.设置初始化通知(必接)
|
.setInitNotifier(new InitNotifier() {
|
|
@Override
|
public void onSuccess() {
|
infoTv.setText("初始化成功");
|
}
|
|
@Override
|
public void onFailed(String message, String trace) {
|
infoTv.setText("初始化失败:" + message);
|
}
|
})
|
// 2.设置登录通知(必接)
|
.setLoginNotifier(new LoginNotifier() {
|
|
@Override
|
public void onSuccess(UserInfo userInfo) {
|
if (userInfo != null) {
|
infoTv.setText("登陆成功" + "\n\r" + "UserID: " + userInfo.getUID() + "\n\r" + "UserName: " + userInfo.getUserName() + "\n\r"
|
+ "Token: " + userInfo.getToken());
|
|
// 登录成功之后,进入游戏时,需要向渠道提交用户信息
|
setUserInfo();
|
}
|
}
|
|
@Override
|
public void onCancel() {
|
infoTv.setText("取消登陆");
|
}
|
|
@Override
|
public void onFailed(final String message, String trace) {
|
infoTv.setText("登陆失败:" + message);
|
}
|
|
})
|
// 3.设置注销通知(必接)
|
.setLogoutNotifier(new LogoutNotifier() {
|
|
@Override
|
public void onSuccess() {
|
infoTv.setText("注销成功");
|
}
|
|
@Override
|
public void onFailed(String message, String trace) {
|
infoTv.setText("注销失败:" + message);
|
}
|
})
|
// 4.设置切换账号通知(必接)
|
.setSwitchAccountNotifier(new SwitchAccountNotifier() {
|
|
@Override
|
public void onSuccess(UserInfo userInfo) {
|
if (userInfo != null) {
|
infoTv.setText("切换账号成功" + "\n\r" + "UserID: " + userInfo.getUID() + "\n\r" + "UserName: " + userInfo.getUserName() + "\n\r"
|
+ "Token: " + userInfo.getToken());
|
}
|
}
|
|
@Override
|
public void onFailed(String message, String trace) {
|
infoTv.setText("切换账号失败:" + message);
|
}
|
|
@Override
|
public void onCancel() {
|
infoTv.setText("取消切换账号");
|
}
|
})
|
// 5.设置支付通知(必接)
|
.setPayNotifier(new PayNotifier() {
|
|
@Override
|
public void onSuccess(String sdkOrderID, String cpOrderID, String extrasParams) {
|
infoTv.setText("支付成功,sdkOrderID:" + sdkOrderID + ",cpOrderID:" + cpOrderID);
|
}
|
|
@Override
|
public void onCancel(String cpOrderID) {
|
infoTv.setText("支付取消,cpOrderID:" + cpOrderID);
|
}
|
|
@Override
|
public void onFailed(String cpOrderID, String message, String trace) {
|
infoTv.setText("支付失败:" + "pay failed,cpOrderID:" + cpOrderID + ",message:" + message);
|
}
|
})
|
// 6.设置退出通知(必接)
|
.setExitNotifier(new ExitNotifier() {
|
|
@Override
|
public void onSuccess() {
|
// 进行游戏本身的退出操作,下面的finish()只是示例
|
finish();
|
}
|
|
@Override
|
public void onFailed(String message, String trace) {
|
infoTv.setText("退出失败:" + message);
|
}
|
});
|
}
|
|
}
|