当前位置:   article > 正文

java 调用 sdk_java 反射调用支付SDK

java如何调用安卓sdk

在android开发中会遇到各种SDK的接入,很是麻烦。最初在想能不能把所有的SDK都 融合到一个当中,发现有点异想天开。但是也可以解决SDK资源不小心没有引入,导致程序调用接口崩溃问题。经过查资料,还是写了一个小Demo,仅供参 考!很早之前写的了,估计移动基地SDK,有变动,不过道理是一样的。

仅以移动基地SDK举例。

1.移动支付需要的SO文件导入

public class CarrotApplication extendsApplication {//是否含有移动支付SDK

boolean useCMBilling = false;

@Overridepublic 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.初始化移动基地支付

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 exception

Log.e("init_cmcc异常捕捉", "异常:"+e.toString());

}

}

3.上面 invokeStaticMethod 的实现

/*** 执行某个类的静态方法

*@paramclassName 类名

*@parammethodName 方法名

*@paramoArray 方法参数

*@paramparamTypeArray 构造参数类型

*@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.调用支付接口

/*** 执行移动支付

*@paramisUsesim sim卡是否可用

*@paramisRepeat 计费点是否可重复

*@paramindex 计费点编号

*@paramorder 订单号

*@paramcallback 支付回调*/

protected void cmcc_pay(boolean isUsesim,boolean isRepeat,String index,String order,finalCarrotPayCallBack callback){try{//这里的 callback 是自定义的回调函数,会在步骤7里介绍

cmcc_PayendHandler = newCarrotHandler(CarrotPaySDK.mContext){

@Overridepublic voidhandleMessage(Message msg) {//TODO Auto-generated method stub

super.handleMessage(msg);//这里处理返回结果

callback.onPayEnd(msg.what);

}

};//传入参数

Object [] cmcc_pay_param = {CarrotPaySDK.mContext,isUsesim,isRepeat,index,order,newObject()};//参数对应的TYPE

Class> [] cmcc_pay_paramtyp = {Context.class,boolean.class,boolean.class,String.class,String.class,Object.class};//"IPayCallback" 是移动支付的回调函数名

cfcaim.invokeContainsInterfaceStaticMethod("cn.cmgame.billing.api.GameInterface", "doBilling", cmcc_pay_param,cmcc_pay_paramtyp,"IPayCallback",CarrotPaySdkFinal.CMCC_PAY);

}catch(Exception e) {//TODO: handle exception

Log.e("cmcc_pay异常捕捉", "异常:"+e.toString());

}

}

5.重点就是上面 invokeContainsInterfaceStaticMethod 方法的实现

/*** 执行包含接口的静态方法

*@paramclassName 类名

*@parammethodName 方法名

*@paramoArray 具体参数

*@paramparamTypeArray 构造参数类型

*@paraminterfaceName 接口名

*@authorliudb

**/

publicObject invokeContainsInterfaceStaticMethod(String className, String methodName,

Object[] oArray,Class> [] paramTypeArray,String interfaceName,CarrotPaySdkFinal payType) throwsException {

Class> ownerClass =Class.forName(className);

Class>[] argsClass = newClass[oArray.length];

Method [] ms=ownerClass.getDeclaredMethods();for (int i = 0, j = oArray.length; i < j; i++) {

argsClass[i]=oArray[i].getClass();

}

Method method=findMethod(ms, methodName, paramTypeArray, interfaceName);

Class> clazz =Class.forName(othre_callBack.getName());//因为接口的最后一个参数是回调函数,所以要设置监测回调

oArray[oArray.length -1] =Proxy.newProxyInstance(

clazz.getClassLoader(),newClass[]{clazz},newCarrotPayCallbackMethodInterceptor(payType));

othre_callBack= null;returnmethod.invoke(ownerClass, oArray);

}

6.还有如何捕获回调的接口返回的参数 CarrotPayCallbackMethodInterceptor 类的实现

public class CarrotPayCallbackMethodInterceptor implementsInvocationHandler {

CarrotPaySdkFinal csf;publicCarrotPayCallbackMethodInterceptor (CarrotPaySdkFinal paytype){

csf=paytype;

}

@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throwsThrowable {//TODO Auto-generated method stub

switch(csf) {caseCMCC_PAY://在这里获得回调函数返回的参数,通过自定义carrotSdkHelper发送给callback处理返回结果

for(int j = 0; j < args.length;j++){if(args[j].getClass().getName().equalsIgnoreCase("java.lang.Integer")){

Integer result=(Integer)args[j];switch(result) {case 1:

Message msg_cmcc_success= newMessage();

msg_cmcc_success.what=CarrotPaySDK.CARROT_PAY_SUCCESS;

CarrotPaySDK.carrotSdkHleper.cmcc_PayendHandler.sendMessage(msg_cmcc_success);break;case 2:

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(int j = 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)callback 类的介绍

public interfaceCarrotPayCallBack {public void onPayEnd(intisSuccess);

}

(2)具体实现新建一个CarrotPayCallBack,传到4步骤的方法里

newCarrotPayCallBack() {

@Overridepublic void onPayEnd(intisSuccess) {//TODO Auto-generated method stub

switch(isSuccess) {caseCarrotPaySDK.CARROT_PAY_SUCCESS:

Toast.makeText(getApplicationContext(),"支付成功", Toast.LENGTH_SHORT).show();break;caseCarrotPaySDK.CARROT_PAY_CANCEL:

Toast.makeText(getApplicationContext(),"支付取消", Toast.LENGTH_SHORT).show();break;caseCarrotPaySDK.CARROT_PAY_FAILD:

Toast.makeText(getApplicationContext(),"支付失败", Toast.LENGTH_SHORT).show();break;caseCarrotPaySDK.CARROT_PAY_UNKNOW:

Toast.makeText(getApplicationContext(),"未知错误", Toast.LENGTH_SHORT).show();break;default:break;

}

}

}

8.代码已经提交到github,如有需要请移驾 https://github.com/liudb5/MyTestPay

9.已经在CSDN发过这个 http://blog.csdn.net/liudb5/article/details/49739873

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/627239
推荐阅读
相关标签
  

闽ICP备14008679号