zoukankan      html  css  js  c++  java
  • cocos2dx安卓客户端接入移动MM付费SDK

    C++和java的调用需要使用JNI,这里使用cocos2dx提供的JniHelper就可以满足要求。

    以游戏中购买炸弹道具为例

    流程图:

    先按照文档进行配置;

    在Java工程中新建一个包"IAP",作为C++与mmsdk交互的中间层;

    Handler和Listener可以使用Demo自带的两个类,将Demo2.4.2中的IAPHandler.java和IAPListener.java复制到IAP中;

    在package IAP下新建类IAPJni

     1 package IAP;
     2 
     3 import com.test.iap.testiap;
     4 
     5 import android.content.Context;
     6 import mm.purchasesdk.OnPurchaseListener;
     7 import mm.purchasesdk.Purchase;
     8 
     9 public class IAPJni
    10 {
    11     private static final String APPID = "11111111";
    12     private static final String APPKEY = "111111111";
    13     
    14     
    15     
    16     public static Purchase purchase;
    17     private static String mPaycode;
    18     private static IAPListener mListener;
    19     private static Context mcontext;
    20     private static testiap mactivity;
    21     public IAPJni(testiap activity)
    22     {
    23         mcontext=activity;
    24         
    25         mactivity=activity;
    26 
    27         IAPHandler iapHandler = new IAPHandler(mactivity);
    28         mListener = new IAPListener(mactivity, iapHandler);
    29         purchase=Purchase.getInstance();
    30         try {
    31             purchase.setAppInfo(APPID, APPKEY);
    32 
    33         } catch (Exception e1) {
    34             e1.printStackTrace();
    35         }
    36 
    37 
    38         
    39     }
    40     public static void orderBomb() {
    41         
    42             
    43             mPaycode="111111111111111";
    44             purchase.order(mactivity,mPaycode,1,false,mListener);\MMSDK
    45     
    46     }
    47     
    48     
    49 
    50     public native static void orderBombSuccess();\调用C++
    51     
    52     public native static void orderFaild();\调用C++
    53 }

     修改IAPListener.java

     1 @Override
     2     public void onBillingFinish(int code, HashMap arg1) {
     3         Log.d(TAG, "billing finish, status code = " + code);
     4         String result = "订购结果:订购成功";
     5         Message message = iapHandler.obtainMessage(IAPHandler.BILL_FINISH);
     6         // 此次订购的orderID
     7         String orderID = null;
     8         // 商品的paycode
     9         String paycode = null;
    10         // 商品的有效期(仅租赁类型商品有�?
    11         String leftday = null;
    12         // 商品的交�?ID,用户可以根据这个交易ID,查询商品是否已经交�?
    13         String tradeID = null;
    14         
    15         String ordertype = null;
    16         if (code == PurchaseCode.ORDER_OK || (code == PurchaseCode.AUTH_OK)) {
    17             /**
    18              * 商品购买成功或�?已经购买�?此时会返回商品的paycode,orderID,以及剩余时间(租赁类型商品)
    19              */
    20             
    21             
    22             if (arg1 != null) {
    23                 leftday = (String) arg1.get(OnPurchaseListener.LEFTDAY);
    24                 if (leftday != null && leftday.trim().length() != 0) {
    25                     result = result + ",剩余时间 �?" + leftday;
    26                 }
    27                 orderID = (String) arg1.get(OnPurchaseListener.ORDERID);
    28                 if (orderID != null && orderID.trim().length() != 0) {
    29                     result = result + ",OrderID �?" + orderID;
    30                 }
    31                 paycode = (String) arg1.get(OnPurchaseListener.PAYCODE);
    32                 if (paycode != null && paycode.trim().length() != 0) {
    33                     result = result + ",Paycode:" + paycode;
    34                 }
    35                 tradeID = (String) arg1.get(OnPurchaseListener.TRADEID);
    36                 if (tradeID != null && tradeID.trim().length() != 0) {
    37                     result = result + ",tradeID:" + tradeID;
    38                 }
    39                 ordertype = (String) arg1.get(OnPurchaseListener.ORDERTYPE);
    40                 if (tradeID != null && tradeID.trim().length() != 0) {
    41                     result = result + ",ORDERTYPE:" + ordertype;
    42                 }
    43 //_________修改___________________________            
    44                 if(paycode.equals(ORDERBOMB)) 
    45                 {
    46                 IAPJni.orderBombSuccess();//返回结果调用
    47                 }
    48             
    49                 
    50             }
    51         } else {
    52             /**
    53              * 表示订购失败�?
    54              */
    55             IAPJni.orderFaild();
    56             result = "订购结果" + Purchase.getReason(code);
    57         }
    58     //    context.dismissProgressDialog();
    59         System.out.println(result);
    60 
    61     }

    C++端:

      购买入口:

      

    void Sample::buyBomb()
    {
        JniMethodInfo jmi;
                        if(JniHelper::getStaticMethodInfo(jmi ,"IAP/IAPJni" ,"orderBomb" ,"()V"))
                        {
                            jmi.env->CallStaticVoidMethod(jmi.classID , jmi.methodID );
                        }
    }

        购买结果回调:

      

     1 #include "platformandroidjniJniHelper.h"
     2 
     3 extern "C"
     4 {
     5     void Java_IAP_IAPJni_orderBombSuccess(JNIEnv *env,jobject thiz)
     6     {
     7         BombNum++;
     8     }
     9     
    10     void Java_IAP_IAPJni_orderFaild()
    11     {
    12                
    13         }
    14     
    15     
    16 }

    配置时需要注意的问题:

    将libidentifyApp.so和libmmSDKjni.so复制到proj.androidjnihellocpp下

    在Android.mk中加入:

    include $(CLEAR_VARS)
    LOCAL_MODULE := libidentifyapp
    LOCAL_SRC_FILES := hellocpp/libidentifyapp.so
    
    include $(PREBUILT_SHARED_LIBRARY)
    
    include $(CLEAR_VARS)
    LOCAL_MODULE := libcasdkjni
    LOCAL_SRC_FILES := hellocpp/libcasdkjni.so
    include $(PREBUILT_SHARED_LIBRARY)
    
    LOCAL_SHARED_LIBRARIES := libidentifyapp
    LOCAL_SHARED_LIBRARIES := libcasdkjni
    include $(BUILD_SHARED_LIBRARY)

    在主Activity中加载sdk的库

      static {
             System.loadLibrary("identifyapp");
             System.loadLibrary("casdkjni");
       
       
        }  

    本例使用的移动MM付费SDK为2.4.2版。

  • 相关阅读:
    angular6 input节流
    遇到跨域别害怕,我来帮你搞定它
    angular4,6 引入第三方插件的方法
    带有动画的字体
    angular 多端打包
    css 常用布局
    chrome 49 版本 跨越 --args --disable-web-security --user-data-dir
    JavaScript中的document.cookie的使用
    ios/iphone手机请求微信用户头像错位BUG及解决方法
    Safari 前端开发调试 iOS 完美解决方案
  • 原文地址:https://www.cnblogs.com/codingdiary/p/3822363.html
Copyright © 2011-2022 走看看