
在Android开发中,您将遇到各种SDK访问支付sdk,这非常麻烦. 起初,我想知道是否可以将所有SDK集成到一个中支付sdk,并发现它有些古怪. 但是,它也可以解决SDK资源没有被意外引入,导致程序调用界面崩溃的问题. 在检查了信息之后,我仍然写了一个小演示,仅供参考!很久以前写过,估计移动基础SDK已更改,但是原因是相同的.
仅以移动基础SDK为例.
1. 导入移动支付所需的SO文件
public class CarrotApplication extends Application { //是否含有移动支付SDK boolean useCMBilling = false; @Override public void onCreate() { // 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 void init_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的实现
/** * 执行某个类的静态方法 * @param className 类名 * @param methodName 方法名 * @param oArray 方法参数 * @param paramTypeArray 构造参数类型 * @author liudb * */ public Object invokeStaticMethod(String className, String methodName, Object[] oArray,Class<?> [] paramTypeArray) throws Exception { Class<?> ownerClass = Class.forName(className); Method method = ownerClass.getMethod(methodName, paramTypeArray); return method.invoke(ownerClass, oArray); }

4. 调用付款界面
/** * 执行移动支付 * @param isUsesim sim卡是否可用 * @param isRepeat 计费点是否可重复 * @param index 计费点编号 * @param order 订单号 * @param callback 支付回调 */ protected void cmcc_pay(boolean isUsesim,boolean isRepeat,String index,String order,final CarrotPayCallBack callback){ try { //这里的 callback 是自定义的回调函数,会在步骤7里介绍 cmcc_PayendHandler = new CarrotHandler(CarrotPaySDK.mContext){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); //这里处理返回结果 callback.onPayEnd(msg.what); } }; // 传入参数 Object [] cmcc_pay_param = {CarrotPaySDK.mContext,isUsesim,isRepeat,index,order,new Object()}; // 参数对应的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方法的实现
/** * 执行包含接口的静态方法 * @param className 类名 * @param methodName 方法名 * @param oArray 具体参数 * @param paramTypeArray 构造参数类型 * @param interfaceName 接口名 * @author liudb * */ public Object invokeContainsInterfaceStaticMethod(String className, String methodName, Object[] oArray,Class<?> [] paramTypeArray,String interfaceName,CarrotPaySdkFinal payType) throws Exception { Class<?> ownerClass = Class.forName(className); Class<?>[] argsClass = new Class[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(), new Class[]{clazz}, new CarrotPayCallbackMethodInterceptor(payType)); othre_callBack = null; return method.invoke(ownerClass, oArray); }
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-277409-1.html
可以么
我们该怎么办呀
表白Amber和秀晶