cocos2dx android工程接入联通Unipay sdk

前端之家收集整理的这篇文章主要介绍了cocos2dx android工程接入联通Unipay sdk前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://blog.csdn.net/xiechyun/article/details/45076311

版本:UnipaySDK_2.0.1_不带账户_三网_2.0.1U2111B0319

中国联通沃商店UniPay支付SDK开发者帮助文档V2.0.1

开发者社区


参考帮助文档


1、将SDK添加到工程

本SDK包,包含如下jar包:

Multimode_UniPay_base.jar(基本jar)

miniapay.jar(电信支付)

alipaysdk.jar(支付宝用)

MobileSecSdk.jar(支付宝用)

utdid4all-1.0.4.jar(支付宝用)

2将这些包放入工程libs目录

如果您的工程已经包含支付宝的jar包,那就保留最新版本的jar包,将旧的jar包删除


3、
在EclipseAndroid工程,可以参照下面的步骤添加jar包:

1)在“Package Explore页签中右击你的工程并选择“Properties

2)在左侧面板中选择“Java Build Path

3)在主窗口中选择“Libraries页签;

4)点击“Add JARs…”按钮;

5)选择这些jar包

6)点击“OK完成。


4、
拷贝assets目录下的资源到工程的assets目录下(移动支付)


5、拷贝armeabi目录下的so到工程libs/armeabi目录下(电信和移动支付)


6、拷贝res目录下的资源到工程res对应的目录下(电信和移动支付)

7、配置AndroidManifest.xml文件

文件如下:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="你的包名"
  4. android:versionCode="19"
  5. android:versionName="1.1.9"
  6. android:installLocation="preferExternal">
  7. uses-sdkandroid:minSdkVersion="9"/>
  8. uses-featureandroid:glEsVersion="0x00020000"/>
  9. uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"uses-permissionandroid:name="android.permission.INTERNET"uses-permissionandroid:name="android.permission.SEND_SMS"uses-permissionandroid:name="android.permission.READ_PHONE_STATE"uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"uses-permissionandroid:name="android.permission.DISABLE_KEYGUARD"uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"application
  10. android:name="你的包名.MyApplication"
  11. android:allowBackup="true"
  12. android:label="@string/app_name"
  13. android:icon="@drawable/icon" <!--TellCocos2dxActivitythenameofour.so-->
  14. Meta-dataandroid:name="android.app.lib_name"
  15. android:value="cocos2dcpp"activityandroid:name="com.unicom.dcLoader.welcomeview"
  16. android:screenOrientation="portrait"
  17. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  18. android:configChanges="keyboardHidden|screenSize|navigation|orientation"intent-filter>
  19. actionandroid:name="android.intent.action.MAIN"categoryandroid:name="android.intent.category.LAUNCHER"</Meta-dataandroid:name="UNICOM_DIST_ACTIVITY"android:value="org.cocos2dx.cpp.AppActivity"activityactivityandroid:name="org.cocos2dx.cpp.AppActivity"
  20. android:configChanges="orientation"serviceandroid:name="com.unicom.wostore.unipay.paysecurity.SecurityServiceFramework"actionandroid:name="你的包名.unicom.wostore.unipay.securityserviceframework"serviceMeta-dataandroid:name="wostore_billing_otherpay"android:value="false"Meta-dataandroid:name="wostore_billing_chinaunicom"android:value="true"Meta-dataandroid:name="wostore_billing_chinamobile"android:value="true"Meta-dataandroid:name="wostore_billing_chinatele"android:value="true"applicationsupports-screensandroid:anyDensity="true"
  21. android:smallScreens="true"
  22. android:normalScreens="true"
  23. android:largeScreens="true"
  24. android:xlargeScreens="true"manifest>

注意:你的包名 此处填写你的工程的包名

8、初始化sdk,调用计费接口

注意事项:

初始化接口必须在Application onCreate方法调用,不可在Activity的onCreate方法调用

1)、cocos2dx创建的工程在src下一般只有org.cocos2dx.cpp包,在src下再创建一个包,包名为你的游戏包名,再在此包下创建MyApplication.java文件

内容如下:

copy

    package你的包名;
  1. importandroid.app.ActivityManager;
  2. importandroid.app.Application;
  3. importandroid.content.Context;
  4. importandroid.util.Log;
  5. importcom.unicom.dcLoader.Utils;
  6. importcom.unicom.dcLoader.Utils.UnipayPayResultListener;
  7. publicclassMyApplicationextendsApplication
  8. {
  9. @Override
  10. publicvoidonCreate()
  11. super.onCreate();
  12. System.loadLibrary("megjb");
  13. Utils.getInstances().initSDK(this,newUnipayPayResultListener()
  14. @Override
  15. publicvoidPayResult(Stringarg0,intarg1,intarg2,Stringarg3)
  16. {
  17. }
  18. });
  19. }

2)、org.cocos2dx.cpp包下的AppActivity.java 文件内容如下:

copy

    packageorg.cocos2dx.cpp;
  1. importorg.cocos2dx.lib.Cocos2dxActivity;
  2. importorg.cocos2dx.lib.Cocos2dxGLSurfaceView;
  3. importorg.cocos2dx.lib.Cocos2dxHandler;
  4. importorg.cocos2dx.lib.Cocos2dxHelper;
  5. importcom.unicom.dcLoader.Utils;
  6. importcom.unicom.dcLoader.Utils.UnipayPayResultListener;
  7. importandroid.content.Intent;
  8. importandroid.os.Bundle;
  9. importandroid.os.Handler;
  10. importandroid.os.Message;
  11. importandroid.widget.Toast;
  12. publicclassAppActivityextendsCocos2dxActivity
  13. publicstaticAppActivitypayActivity=null;
  14. privatestaticStringjIndex="";
  15. protectedvoidonCreate(BundlesavedInstanceState)
  16. //TODOAuto-generatedmethodstub
  17. super.onCreate(savedInstanceState);
  18. payActivity=this;
  19. }
  20. //返回实例
  21. publicstaticObjectgetInstance()
  22. if(payActivity==null)
  23. payActivity=newAppActivity();
  24. returnpayActivity;
  25. privateclasspayCallbackimplementsUnipayPayResultListener
  26. publicvoidPayResult(Stringpaycode,intflag,intflag2,Stringerror)
  27. //flag为支付回调结果,flag2为回调类型,error为当前结果描述,paycode是完整的支付编码
  28. switch(flag)
  29. case1://success
  30. //此处放置支付请求已提交的相关处理代码
  31. runNativeCallback("1",jIndex);
  32. break;
  33. case2://fail
  34. runNativeCallback("2",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> case3://cancel
  35. runNativeCallback("0",248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> default:
  36. break;
  37. publicstaticvoidrunNativeCallback(finalStringstate,finalStringbillingIndex)
  38. Cocos2dxGLSurfaceView.getInstance().queueEvent(newRunnable()
  39. publicvoidrun()
  40. callBack(state,billingIndex);
  41. });
  42. //根据返回结果,调用不同的处理交易标志:istate0:取消,1成功,2失败;
  43. publicnativestaticvoidcallBack(Stringstate,StringbillingIndex);
  44. //调用jdk的函数付款
  45. publicvoidjdkPay(StringbillingIndex)
  46. jIndex=billingIndex;
  47. StringstrSerial="";
  48. intiSerial=1;
  49. iSerial=Integer.parseInt(billingIndex);
  50. payHandler.sendEmptyMessage(iSerial);
  51. privateHandlerpayHandler=newHandler()
  52. publicvoidhandleMessage(android.os.Messagemsg)
  53. //计费点001~015对应联通的计费代码001~015
  54. switch(msg.what)
  55. case1:
  56. Utils.getInstances().pay(payActivity,"001",newpayCallback());
  57. case2:
  58. Utils.getInstances().pay(payActivity,"002",newpayCallback());
  59. ...//此处省略多个case
  60. };
  61. };
  62. protectedvoidonResume()
  63. super.onResume();
  64. Utils.getInstances().onResume(this);
  65. protectedvoidonPause()
  66. super.onPause();
  67. Utils.getInstances().onPause(this);
  68. protectedvoidonDestroy()
  69. super.onDestroy();
  70. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  71. 如果jdkPay函数中直接调用Utils.getInstances().pay函数,会提示 Can't create handler inside thread that has not called Looper.prepare()

    即不能在子线程中刷新ui

    所以新建一个handler。

    9、jni 调用文件文件

    copy

    extern"C"
  1. //参数:...计费点代码字串
  2. voidshowTipDialog(constchar*billIndex);
  3. #endif

10、cpp文件

copy

    #include"../../Classes/Result.h"
  1. #include"jniBridge.h"
  2. #include"cocos2d.h"
  3. #include"platform\android\jni/JniHelper.h"
  4. #includejni.h #defineCLASS_NAME"org/cocos2dx/cpp/AppActivity"
  5. usingnamespacecocos2d;
  6. extern"C"
  7. voidJava_org_cocos2dx_cpp_AppActivity_callBack(JNIEnv*env,jobjectthiz,jstringstate,jstringbillingIndex)
  8. constchar*pState=env->GetStringUTFChars(state,NULL);
  9. constchar*pIndex=env->GetStringUTFChars(billingIndex,NULL);
  10. if(strcmp(pState,"1")==0)
  11. doSuccess(pIndex);//成功后调用函数,在文件头部要#include此函数文件如Result.h
  12. elseif(strcmp(pState,"2")==0)
  13. doFault(pIndex);//失败后调用函数,在文件头部要#include此函数文件如Result.h
  14. {//取消购买时
  15. doCancel(pIndex);//取消后调用函数,在文件头部要#include此函数文件如Result.h
  16. env->ReleaseStringUTFChars(state,pState);
  17. env->ReleaseStringUTFChars(billingIndex,pIndex);
  18. voidshowTipDialog(constchar*billIndex)
  19. JniMethodInfominfo;
  20. jobjectjobj;
  21. if(JniHelper::getStaticMethodInfo(minfo,CLASS_NAME,"getInstance","()Ljava/lang/Object;"))
  22. jobj=minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);
  23. if(JniHelper::getMethodInfo(minfo,"jdkPay","(Ljava/lang/String;)V"))
  24. jstringjindex=minfo.env->NewStringUTF(billIndex);
  25. minfo.env->CallVoidMethod(jobj,minfo.methodID,jindex);
  26. minfo.env->DeleteLocalRef(jindex);
  27. Over

    猜你在找的Cocos2d-x相关文章