简体   繁体   English

带有回调的Android Cordova插件

[英]Android cordova plugin with callback

I am converting a IOS phonegap plugin to android. 我正在将IOS phonegap插件转换为android。 In IOS there is a plugin call like this 在IOS中,有一个像这样的插件调用

cordova.exec('InAppPurchaseManager.requestProductsData', callbackName, {productIds: productIds});

This line of code is using the native layer to fetch the product details from itues store. 此行代码使用本机层从itues商店获取产品详细信息。 Once this message is send to native layer it then makes an asynchronous request to get products details.Once it has the products it then makes the call 该消息发送到本机层后,便发出异步请求以获取产品详细信息。一旦收到产品,便进行呼叫

NSString *js = [NSString stringWithFormat:@"%@.apply(plugins.inAppPurchaseManager, %@);", callback, [callbackArgs cdvjk_JSONSerialize]];
    [command writeJavascript: js];

In above line the variable callback holds the value passed by the argument callbackName when the plugin is invoked. 在上面的代码行中,变量callback保留了调用插件时由参数callbackName传递的值。 How can I do the same in android? 我该如何在android中做同样的事情?

The plugin dont have an option to recieve the callbackname value in android 插件没有选项来接收android中的callbackname值

public PluginResult execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException

In android from phonegap I am using this method 在phonegap的android中,我正在使用此方法

cordova.exec(callbackName, null, "MyPluginClass", "getProducts", productIds);

How can I implement the same in android? 如何在android中实现相同功能?

Thanks 谢谢

You have to use the Google Play Stores In App Billing service. 您必须使用Google Play应用商店内结算服务。 This project on GitHub has some good examples: GitHub上的这个项目有一些很好的例子:

https://github.com/poiuytrez/AndroidInAppBilling https://github.com/poiuytrez/AndroidInAppBilling

I didn't get time to create one code base myself for both IOS and Android but I plan to in the future... What I ended up doing is the following, it probably would have been faster to add a seperate layer but I was just trying to get it to work at the time and will refactor in the future... Here's how I did it which required minimum modification to the Android GitHub project: 我没有时间自己为IOS和Android创建一个代码库,但我计划将来...我最终要做的是以下操作,添加单独的层可能会更快。只是想让它在当时起作用,并在将来进行重构...这是我做到的方法,它需要对Android GitHub项目进行最少的修改:

           if ( window.plugins.InAppPurchasePlugin ) {

            // launch the ios in app purchase MakePurchase function....
            var quantity = 1;

            window.plugins.InAppPurchasePlugin.buyProduct( config.gratitudeIosInAppProductId, quantity, buyProductError );
        }
        else {

            if ( window.plugins.inappbilling ) {

                window.plugins.inappbilling.buy( androidBuySuccessHandler, buyProductError, config.gratitudeAndroidInAppProductId );
            }
            else {

                kendoConsole.log( 'no inappbilling?' );
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM