Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering javascript event from android

I have created a cordova application. I am running an background service to perform some native task in the application. I need to trigger a java-script event once the background service complete its task. Is it possible to trigger js events from android?. Not able to find any solid answers for this. I need events because the application wound wait for the task in background service to complete. I want to event to notify the application that the task is complete. Is there any better way to implement this logic?.

like image 747
sijo jose Avatar asked Aug 16 '26 17:08

sijo jose


1 Answers

Cordova itself doesn't expose its webview properties publicly for use by other Java classes, but you can do this with a minimal Cordova plugin which would allow your background service to access the Cordova webview in order to execute javascript in it from the Java layer. Then it's just a question of injecting the JS to trigger an event.

First you'd create a Cordova plugin to expose the necessary elements of Cordova to your background service:

public class MyPlugin extends CordovaPlugin{
    private static final String TAG = "MyPlugin";
    static MyPlugin instance = null;
    static CordovaWebView cordovaWebView;
    static CordovaInterface cordovaInterface;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

        instance = this;
        cordovaWebView = webView;
        cordovaInterface = cordova;
    }

    @Override
    public void onDestroy() {
        instance = null;
    }

    private static void executeGlobalJavascript(final String jsString) {
        if (instance == null) {return;}
        instance.cordovaInterface.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    instance.cordovaWebView.loadUrl("javascript:" + jsString);
                } catch (Exception e) {
                    Log.e(TAG, "Error executing javascript: "+ e.toString());
                }
            }
        });
    }

    public static void triggerJavascriptEvent(final String eventName){
        executeGlobalJavascript(String.format("document.dispatchEvent(new Event('%s'));", eventName));
    }
}

Then your background service can call the public method exposed by that plugin class:

public class MyService {
    public static void myMethod(){
        MyPlugin.triggerJavascriptEvent("myserviceevent");
    }
}

And finally, in your Cordova app's JS layer, you'd listen for your custom event:

document.addEventListener('myserviceevent', function(){
    console.log("myserviceevent received");
}, false);

I've created an example Cordova project which contains the minimal custom plugins required to achieve this which you can download here: http://ge.tt/8UeL6lu2

Once downloaded, unzip then:

cd cordova-test
cordova platform add android
cordova run android
like image 137
DaveAlden Avatar answered Aug 18 '26 06:08

DaveAlden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!