How to call another cordova plugin from an android

2019-08-11 09:21发布

问题:

What would be the best way to call another cordova plugin in android?

I want to have a background service that calls a server for some information and based on that information, I need to do for example set a badge.

I found a Cordova Background Services Plugin, https://github.com/phpsa/cbsp that could work well for my purposes and the use it with https://github.com/katzer/cordova-plugin-badge among others.

I'm wondering what would be the best way to do it? Can I just include the classes and call them straight from my java code or is there something else that is required?

Looking for guidance on best approach.

EDIT: So apparently this isn't really a option. I managed to include the other plugin in my java code and I was able to call it, but ran into issues with CallbackContext, which is created by cordova platform when calling a plugin from javascript. This was basically a design feature of the badge plugin

The only way I found to implement the features I needed was to take the code from the other plugin and refactor it into my background service.

回答1:

For example:
public class OtherPlugin extends CordovaPlugin{ @Override protected void pluginInitialize() { YourClassPlugin yourClassPlugin = (YourClassPlugin)this.webView.getPluginManager().getPlugin("YourClassPlugin"); } }



回答2:

And why can you use javascript as a glue? Simply passing result of one plugin into another.

But when you have to do it natively, then you should do it via intents and BroadcastReciever. http://developer.android.com/guide/components/intents-filters.html

Keep in my mind that receiving plugin has to have some kind of implicit intent defined. Check tutorial, there is all you need to know. http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html