COCOS2DX Android Client Access mobile mm Subscription SDK

Source: Internet
Author: User

The calls to C + + and Java require JNI, where the jnihelper provided by COCOS2DX can be used to meet the requirements.

Take the game to buy bomb props for example

Flow chart:

First configure according to the document;

Create a new package "IAP" in Java Engineering, as the middle layer of C + + interaction with MMSDK;

handler and listener can use the two classes that the demo comes with, and the Demo2.4.2 Iaphandler.java and Iaplistener.java copied into the IAP;

Create a new class Iapjni under the package IAP

1  PackageIAP;2 3 ImportCom.test.iap.testiap;4 5 ImportAndroid.content.Context;6 ImportMm.purchasesdk.OnPurchaseListener;7 Importmm.purchasesdk.Purchase;8 9  Public classIapjniTen { One     Private Static FinalString APPID = "11111111"; A     Private Static FinalString APPKEY = "111111111"; -      -      the      -      Public StaticPurchase Purchase; -     Private StaticString Mpaycode; -     Private StaticIaplistener Mlistener; +     Private StaticContext Mcontext; -     Private StaticTESTIAP mactivity; +      PublicIapjni (TESTIAP activity) A     { atmcontext=activity; -          -mactivity=activity; -  -Iaphandler Iaphandler =NewIaphandler (mactivity); -Mlistener =NewIaplistener (mactivity, iaphandler); inPurchase=purchase.getinstance (); -         Try { to Purchase.setappinfo (APPID, APPKEY); +  -}Catch(Exception E1) { the e1.printstacktrace (); *         } $ Panax Notoginseng  -          the     } +      Public Static voidOrderbomb () { A          the              +Mpaycode= "111111111111111"; -Purchase.order (mactivity,mpaycode,1,false, Mlistener); \\MMSDK $      $     } -      -      the  -      Public native Static voidOrderbombsuccess (); \ \ call C + +Wuyi      the      Public native Static voidOrderfaild (); \ \ call C + + -}

Modify Iaplistener.java

1 @Override2      Public voidOnbillingfinish (intcode, HASHMAP arg1) {3LOG.D (TAG, "Billing finish, Status code =" +code);4String result = "Order Result: Order succeeded";5Message message =iaphandler.obtainmessage (iaphandler.bill_finish);6         //OrderID of this order7String OrderID =NULL;8         //Paycode of Goods9String paycode =NULL;Ten         //the expiry date of the product (only the type of goods are available?? OneString Leftday =NULL; A         //the delivery of goods?? ID, the user can be based on this transaction ID, check whether the goods have been handed?? -String TradeID =NULL; -          theString OrderType =NULL; -         if(Code = = PURCHASECODE.ORDER_OK | | (Code = =PURCHASECODE.AUTH_OK)) { -             /** - * Product purchase success or?? Already purchased?? This will return the Paycode,orderid of the item and the time remaining (rental type of product) +              */ -              +              A             if(Arg1! =NULL) { atLeftday =(String) arg1.get (onpurchaselistener.leftday); -                 if(Leftday! =NULL&& Leftday.trim (). Length ()! = 0) { -result = result + ", time remaining??" +Leftday; -                 } -OrderID =(String) arg1.get (Onpurchaselistener.orderid); -                 if(OrderID! =NULL&& Orderid.trim (). Length ()! = 0) { inresult = result + ", OrderID??" +OrderID; -                 } toPaycode =(String) arg1.get (onpurchaselistener.paycode); +                 if(Paycode! =NULL&& Paycode.trim (). Length ()! = 0) { -result = result + ", Paycode:" +Paycode; the                 } *TradeID =(String) arg1.get (Onpurchaselistener.tradeid); $                 if(TradeID! =NULL&& Tradeid.trim (). Length ()! = 0) {Panax Notoginsengresult = result + ", TradeID:" +TradeID; -                 } theOrderType =(String) arg1.get (onpurchaselistener.ordertype); +                 if(TradeID! =NULL&& Tradeid.trim (). Length ()! = 0) { Aresult = result + ", OrderType:" +OrderType; the                 } + //_________ Modifying ___________________________ -                 if(Paycode.equals (orderbomb)) $                 { $Iapjni.orderbombsuccess ();//return result Call -                 } -              the                  -             }Wuyi}Else { the             /** - * Indicates a failed subscription?? Wu              */ - iapjni.orderfaild (); Aboutresult = "Order Result" +Purchase.getreason (code); $         } -     //Context.dismissprogressdialog (); - System.out.println (result); -  A}

C + + side:

Purchase entrance:

  

void Sample::buybomb () {    jnimethodinfo jmi;                     if (Jnihelper::getstaticmethodinfo (JMI,"iap/iapjni" ,"orderbomb" ,"() V"))                    {                        jmi.env,Callstaticvoidmethod ( Jmi.classid, Jmi.methodid);}                    }

Purchase result callback:

  

1#include"Platform\android\jni\jnihelper.h"2 3 extern "C"4 {5     voidJava_iap_iapjni_orderbombsuccess (JNIENV *env,jobject thiz)6     {7bombnum++;8     }9     Ten     voidJava_iap_iapjni_orderfaild () One     { A                 -         } -      the      -}

Issues to be aware of when configuring:

Copy libidentifyapp.so and libmmsdkjni.so to Proj.android\jni\hellocpp

Add in 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)

Loading the library of the SDK in the main activity

  Static {         system.loadlibrary ("identifyapp");         System.loadlibrary ("casdkjni");          }  

This example uses the mobile mm paid SDK to be 2.4.2 version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.