In the development of Android will encounter a variety of SDK access, it is very troublesome. I was wondering if I could integrate all the SDKs into one and find a little whimsical. However, it is also possible to resolve the problem that the SDK resources are inadvertently not introduced, causing the program to invoke interface crashes. After checking the data, or wrote a small demo, only for reference! It was written a long time ago, estimated mobile Base SDK, there are changes, but the truth is the same.
Just use the mobile base SDK for example.
1. Import of so files required for mobile payment
Public classCarrotapplicationextendsApplication {//whether the mobile payment SDK is included BooleanUsecmbilling =false; @Override Public voidonCreate () {//TODO auto-generated Method Stub Try{class.forname ("Cn.cmgame.billing.api.GameInterface"); Usecmbilling=true; } Catch(ClassNotFoundException ignored) {}if(usecmbilling) {system.loadlibrary ("MEGJB"); } } }
2. Initialize the mobile base payment
protected voidINIT_CMCC (String app_name, String App_company, String telphone_number) {Try{Object [] cmcc_init_obj={Carrotpaysdk.mcontext,app_name,app_company,telphone_number}; Class<?> [] Classparam = {Activity.class, String.class, String.class, String.class}; Cfcaim.invokestaticmethod ("Cn.cmgame.billing.api.GameInterface", "InitializeApp", Cmcc_init_obj,classparam); } Catch(Exception e) {//Todo:handle ExceptionLOG.E ("INIT_CMCC exception Catch", "Exception:" +e.tostring ()); } }
3. Implementation of the above Invokestaticmethod
/*** Execute a static method of a class *@paramClassName class name *@paramMethodName Method Name *@paramOarray Method Parameters *@paramParamtypearray Construction Parameter type *@authorLiudb **/ PublicObject Invokestaticmethod (String className, String methodName, object[] Oarray,class<?> [] paramtypearray)throwsException {Class<?> Ownerclass =Class.forName (className); Method Method=Ownerclass.getmethod (MethodName, Paramtypearray); returnMethod.invoke (Ownerclass, Oarray); }
4. Calling the Payment interface
/*** Perform mobile payment *@paramIsusesim SIM card available *@paramIsrepeat If the billing point is repeatable *@paramIndex Billing point number *@paramOrder Number *@paramCallback Payment Callback*/ protected voidCmcc_pay (BooleanIsusesim,BooleanIsrepeat,string index,string Order,FinalCarrotpaycallback Callback) { Try { //The callback here is a custom callback function, which is described in step 7Cmcc_payendhandler =NewCarrothandler (carrotpaysdk.mcontext) {@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method Stub Super. Handlemessage (msg); //The return result is processed herecallback.onpayend (msg.what); } }; //Incoming ParametersObject [] Cmcc_pay_param = {Carrotpaysdk.mcontext,isusesim,isrepeat,index,order,NewObject ()}; //parameter corresponds to the typeclass<?> [] Cmcc_pay_paramtyp = {Context.class,Boolean.class,Boolean.class, String.class, String.class, Object.class}; //"Ipaycallback" is the name of the callback function for the mobile payment.Cfcaim.invokecontainsinterfacestaticmethod ("Cn.cmgame.billing.api.GameInterface", "dobilling", Cmcc_pay_param, Cmcc_pay_paramtyp, "Ipaycallback", Carrotpaysdkfinal.cmcc_pay); } Catch(Exception e) {//Todo:handle ExceptionLOG.E ("Cmcc_pay exception Catch", "Exception:" +e.tostring ()); } }
5. The focus is on the implementation of the above Invokecontainsinterfacestaticmethod method
/*** Execution of static methods containing interfaces *@paramClassName class name *@paramMethodName Method Name *@paramOarray Specific Parameters *@paramParamtypearray Construction Parameter type *@paramInterfaceName Interface Name *@authorLiudb **/ PublicObject Invokecontainsinterfacestaticmethod (String className, String methodName, object[] Oarray,clas S<?> [] paramtypearray,string interfacename,carrotpaysdkfinal PayType)throwsException {Class<?> Ownerclass =Class.forName (className); Class<?>[] Argsclass =NewClass[oarray.length]; Method [] Ms=Ownerclass.getdeclaredmethods (); for(inti = 0, j = oarray.length; I < J; i++) {Argsclass[i]=Oarray[i].getclass (); } Method=FindMethod (MS, MethodName, Paramtypearray, InterfaceName); Class<?> Clazz =Class.forName (Othre_callback.getname ()); //because the last parameter of the interface is a callback function, you set the monitoring callbackOARRAY[OARRAY.LENGTH-1] =proxy.newproxyinstance (Clazz.getclassloader (),NewClass[]{clazz},NewCarrotpaycallbackmethodinterceptor (PayType)); Othre_callback=NULL; returnMethod.invoke (Ownerclass, Oarray); }
6. Also the implementation of the Carrotpaycallbackmethodinterceptor class that returns the parameters of how the callback interface is captured
Public classCarrotpaycallbackmethodinterceptorImplementsInvocationhandler {carrotpaysdkfinal CSF; Publiccarrotpaycallbackmethodinterceptor (carrotpaysdkfinal paytype) {CSF=PayType; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method Stub Switch(CSF) { CaseCmcc_pay://here we get the parameters returned by the callback function, sent to callback by the custom Carrotsdkhelper to process the returned results for(intj = 0; J < args.length;j++){ if(Args[j].getclass (). GetName (). Equalsignorecase ("Java.lang.Integer") {Integer result=(Integer) args[j]; Switch(Result) { Case1: Message msg_cmcc_success=NewMessage (); Msg_cmcc_success.what=carrotpaysdk.carrot_pay_success; CarrotPaySDK.carrotSdkHleper.cmcc_PayendHandler.sendMessage (msg_cmcc_success); Break; Case2: Message msg_cmcc_faild=NewMessage (); Msg_cmcc_faild.what=Carrotpaysdk.carrot_pay_faild; CarrotPaySDK.carrotSdkHleper.cmcc_PayendHandler.sendMessage (Msg_cmcc_faild); Break; default: Message msg_cmcc_faild2=NewMessage (); Msg_cmcc_faild2.what=Carrotpaysdk.carrot_pay_faild; CarrotPaySDK.carrotSdkHleper.cmcc_PayendHandler.sendMessage (MSG_CMCC_FAILD2); Break; } } } Break; CaseOTHER_PAY:LOG.E ("___________other_______________", "____________________" +method.getname ()); for(intj = 0; J < args.length;j++) {LOG.E ("___________other_______________" +args[j].getclass (). GetName (), "____________________" +args[j].tostring ()); } LOG.E ("___________other_______________", "______333333______________"); Break; default: Break; } return NULL; } }
7. (1) Introduction of callback Class
Public Interface Carrotpaycallback { publicvoid onpayend (int issuccess); }
(2) concrete implementation of a new carrotpaycallback, to 4 steps of the method
NewCarrotpaycallback () {@Override Public voidOnpayend (intissuccess) { //TODO auto-generated Method Stub Switch(issuccess) { CaseCarrotPaySDK.CARROT_PAY_SUCCESS:Toast.makeText (Getapplicationcontext (),"Payment Success", Toast.length_short). Show (); Break; CaseCarrotPaySDK.CARROT_PAY_CANCEL:Toast.makeText (Getapplicationcontext (),"Payment Cancellation", Toast.length_short). Show (); Break; CaseCarrotPaySDK.CARROT_PAY_FAILD:Toast.makeText (Getapplicationcontext (),"Payment Failed", Toast.length_short). Show (); Break; CaseCarrotPaySDK.CARROT_PAY_UNKNOW:Toast.makeText (Getapplicationcontext (),"Unknown Error", Toast.length_short). Show (); Break; default: Break; } } }
8. The code has been submitted to GitHub, please move if necessary https://github.com/liudb5/MyTestPay
9. This http://blog.csdn.net/liudb5/article/details/49739873 has been sent to CSDN.
Java Reflection call Payment SDK