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 "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 := 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版。