cocos2dx安卓用戶端接入移動MM付費SDK

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

 

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 IAPJni10 {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);\\MMSDK45     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 "platform\android\jni\JniHelper.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.android\jni\hellocpp下

在Android.mk中加入:

include $(CLEAR_VARS)LOCAL_MODULE := libidentifyappLOCAL_SRC_FILES := hellocpp/libidentifyapp.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := libcasdkjniLOCAL_SRC_FILES := hellocpp/libcasdkjni.soinclude $(PREBUILT_SHARED_LIBRARY)LOCAL_SHARED_LIBRARIES := libidentifyappLOCAL_SHARED_LIBRARIES := libcasdkjniinclude $(BUILD_SHARED_LIBRARY)

在主Activity中載入sdk的庫

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

 

本例使用的移動MM付費SDK為2.4.2版。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.