【文件属性】:
文件名称:(接入华为sdk for unity) eclipse部分
文件大小:920KB
文件格式:ZIP
更新时间:2020-11-08 07:56:25
eclipse unity
/**
* 初始化SDK
*/
private static void initSDK(String appid, String appkey) {
ProxySelector defaultProxySelector = ProxySelector.getDefault();
Proxy proxy = null;
List proxyList = null;
try {
proxyList = defaultProxySelector.select(new URI(
"http://www.google.it"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
if (proxyList != null && proxyList.size() > 0) {
proxy = proxyList.get(0);
Log.d(TAG, "Current Proxy Configuration: " + proxy.toString());
}
AppInfo appInfo = new AppInfo();
appInfo.setAppId(appid);// 应用ID
appInfo.setAppKey(appkey);// 应用Key
appInfo.setCtx(ctx);
/*
* VersionCheckLevelNormal 版本检查失败可以继续进行游戏 VersionCheckLevelStrict
* 版本检查失败则不能进入游戏 默认取值为VersionCheckLevelStrict
*/
appInfo.setVersionCheckStatus(AppInfo.VERSION_CHECK_LEVEL_STRICT);
// 初始化SDK
Commplatform.getInstance().Init(0, appInfo,
new CallbackListener() {
@Override
public void callback(final int paramInt, Integer paramT) {
ctx.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Init paramInt = " + paramInt);
// ok.setEnabled(true);
LogUtil.send("初始化: " + paramInt);
}
});
}
});
}
/**
* 用户登录
* */
public static void loginEx(Context context) {
Bundle bundle = new Bundle();
bundle.putString("nounce", UUID.randomUUID().toString()
.replace("-", ""));
Commplatform.getInstance().LoginEx(context, bundle,
new CallbackListener() {
@Override
public void callback(int resultCode, Bundle bundle) {
if (resultCode == ErrorCode.COM_PLATFORM_SUCCESS) {
// 完成参数验签
// 处理登录成功逻辑
// HomeActivity.show(ctx);
// String uin= bundle.getString("uin");
LogUtil.send("登录成功 :");
// String uin= bundle.get("nounce").toString();
String uin = Commplatform.getInstance()
.getLoginUin();
LogUtil.send("登录成功 uin :" + uin);
UnityPlayer.UnitySendMessage("MainScript",
"HuaweiLoginBack", uin);
} else {
// 处理登录失败逻辑
LogUtil.send("登录失败");
}
}
});
}
/**
* 充值
* */
public static int pay(String currency) {
isPaying = true;
Payment payment = new Payment();
ProductBean productBean = productMap.get(currency);
makeSerial();
payment.setTradeNo(orderKen);
payment.setProductId(productBean.getProductId());
payment.setSubject(productBean.getTitle());
payment.setDesc(productBean.getDescription());
payment.setAmount(productBean.getPrice_amount());
payment.setCurrency(productBean.getPrice_currency_code());
payment.setNote("");
payment.setNotifyURL("");
payment.setThirdAppId(id);
payment.setThirdAppName("Spot Battle");
payment.setThirdAppPkgname("com.testcrecool.mi");
final String tradeNo = payment.getTradeNo();
final String productId = payment.getProductId();
// 将订单的详细信息插入数据库
PaymentTableAdapter.insert(ctx, payment);
int res = Commplatform.getInstance().UniPayExt(payment, ctx,
new CallbackListener() {
@Override
public void callback(final int code, final PayResult arg1) {
ctx.runOnUiThread(new Runnable() {
public void run() {
// 回调结果,即支付过程结束
isPaying = false;
LogUtil.send("回调结果,即支付过程结束 code: " + code);
if (code == ErrorCode.COM_PLATFORM_SUCCESS) {
// TODO Example 1
// 根据final 的 productID 或者 orderId
// 去处理商品,比如查询道具,发放道具等
// TODO Example 2
// 可以根据订单号查询订单详细信息,在做订单的处理,比如查询道具,发放道具等
// 如下:
Payment payment = PaymentTableAdapter
.queryByOrderId(ctx, orderKen);
// 购买有结果,即删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
orderKen);
Log.i(TAG, "COM_PLATFORM_SUCCESS");
LogUtil.send("成功");
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", orderKen);
} else if (code == ErrorCode.COM_PLATFORM_ERROR_PAY_FAILURE) {
Log.i(TAG, "COM_PLATFORM_ERROR_PAY_FAILURE");
} else if (code == ErrorCode.COM_PLATFORM_ERROR_PAY_CANCEL) {
// 购买失败
// 购买有结果,即删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
tradeNo);
Log.i(TAG, "COM_PLATFORM_ERROR_PAY_CANCEL");
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else if (code == ErrorCode.COM_PLATFORM_ERROR_PAY_CANCEL) {
LogUtil.send("取消购买");
// 取消购买
// 购买有结果,即删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
tradeNo);
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else {
LogUtil.send("Purchase failed. Error code:"
+ code);
Log.i(TAG, "COM_PLATFORM_ERROR_UNKNOWN");
}
}
});
}
});
if (res == 0) {
LogUtil.send("000");
return 0;
} else {
// 返回错误,即支付过程结束
isPaying = false;
LogUtil.send("返回错误,即支付过程结束");
return -1;
}
}
/**
* 获取应用内商品信息
**/
public static void querySkuDetail() {
Commplatform.getInstance().getSkuDetails(ctx,
new CallbackListener>() {
@Override
public void callback(int errorCode,
List skuDetails) {
if (errorCode == ErrorCode.COM_PLATFORM_SUCCESS
&& skuDetails != null) {
for (SkuDetail detail : skuDetails) {
ProductBean prBean = new ProductBean();
prBean.setProductId(detail.productId);
prBean.setPrice(detail.price);
prBean.setPrice_amount(detail.price_amount);
prBean.setPrice_currency_code(detail.price_currency_code);
prBean.setTitle(detail.title);
prBean.setDescription(detail.description);
productMap.put(detail.productId, prBean);
}
// showText.setText(buffer.toString());
LogUtil.send("获取应用内商品信息");
} else {
// showText.setText("query error");
LogUtil.send("query error");
}
}
});
}
/**
* 生成订单号
* */
private static String makeSerial() {
// 生成订单号
orderKen = UUID.randomUUID().toString().replaceAll("-", "");
return orderKen;
}
// 同步支付订单的漏单查询接口调用
private static void checkPay(final Payment paymentSerial) {
QueryPayment queryPayment = new QueryPayment();
queryPayment.setTradeNo(paymentSerial.getTradeNo());
queryPayment.setThirdAppId(paymentSerial.getThirdAppId());
final String tradeNo= queryPayment.getTradeNo();
Commplatform.getInstance().queryPayment(queryPayment, ctx,
new CallbackListener() {
@Override
public void callback(int paramInt, PaymentState paramT) {
if (paramInt == ErrorCode.COM_PLATFORM_SUCCESS) {
// Step2:订单查询成功 从数据库删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,tradeNo);
// 订单支付成功,可以根据订单号查询订单详细信息,在做订单的处理,比如查询道具,发放道具等
// TODO… 游戏代码
LogUtil.send("漏单查询成功!!!");
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", orderKen);
} else if (paramInt == ErrorCode.COM_PLATFORM_ERROR_UNEXIST_ORDER) {
// Step2:订单不存在 从数据库删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
tradeNo);
// 根据游戏自身的体验决定如何处理
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else if (paramInt == ErrorCode.COM_PLATFORM_ERROR_PAY_FAILURE) {
// Step2:订单支付失败 从数据库删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
tradeNo);
// 根据游戏自身的体验决定如何处理
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else if (paramInt == ErrorCode.COM_PLATFORM_ERROR_SERVER_RETURN_ERROR) {
// Step2:服务端返回错误 从数据库删除此订单号
PaymentTableAdapter.deleteByOrderId(ctx,
tradeNo);
// 根据游戏自身的体验决定如何处理
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else if (paramInt == ErrorCode.COM_PLATFORM_ERROR_PAY_REQUEST_SUBMITTED) {
// 订单已提交
// 根据游戏自身的体验决定如何处理
// 后续还需要继续查询
UnityPlayer.UnitySendMessage("MainScript",
"hwPayCallback", "error");
} else {
// 未知错误
// 根据游戏自身的体验决定如何处理
// 后续还需要继续查询
}
}
});
}
【文件预览】:
hwdemo
----bin()
--------res()
--------jarlist.cache(120B)
--------classes()
--------AndroidManifest.xml(4KB)
----res()
--------drawable-ldpi()
--------raw()
--------values-v11()
--------values-v14()
--------drawable-hdpi()
--------drawable-xhdpi()
--------drawable-xxhdpi()
--------values()
--------drawable-mdpi()
--------layout()
--------xml()
----proguard-project.txt(781B)
----ic_launcher-web.png(50KB)
----assets()
----gen()
--------com()
----.settings()
--------org.eclipse.jdt.core.prefs(177B)
----src()
--------com()
----.project(842B)
----.classpath(711B)
----project.properties(636B)
----AndroidManifest.xml(4KB)
----libs()
--------odin.jar(142KB)
--------android-support-v4.jar(543KB)
--------classes.jar(68KB)
--------bridge_sdk_game.jar(142KB)
----lint.xml(53B)