Can JavaScript be accessed from a background thread in a Cordova Android plugin?

In my Cordova application, I am utilizing a native plugin on Android (with plans for other platforms in the future).

The plugin is loaded when the application starts (

<param name="onload" value="true" />
in plugin.xml) and the native code performs tasks within the initialize method (overloaded from the CordovaPlugin class).

After completing work in the initialize method, an event is generated at a later time that needs to be delivered to the javascript API.

I'm wondering if there's a way to asynchronously communicate with the javascript from the native side of the plugin without first initiating communication from the javascript side. (If I were to initiate communication from JS->Java, I would receive a CallbackContext that could be used for callbacks.)

I came across this https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/NativeToJsMessageQueue.java but I'm unsure how to use it as there seems to be no documentation available - I'm also uncertain if it's even meant for public usage.

Can I utilize

webview.loadUrl("javascript: ... ");
to make calls into the javascript side, or will this potentially disrupt or interfere with the cordova framework operating in JS?

I'd like to know if there's a recommended approach for achieving this task and whether it's supported across different platforms (or if the concepts can be applied to other platforms).

Thank you

Answer №1

At the moment, I have implemented a temporary solution where the user calls my plugin (JS->java) and provides the callback function they are registering.

On the Java side, I save the CallbackContext for later use. It's crucial to remember that when using CallbackContext.sendPluginResult, the passed PluginResult must have

PluginResult.setKeepCallback(true)
set. Failing to do so will render the context invalid for future calls after the initial callback.

I haven't encountered any threading issues with this approach and I am optimistic that it may be applicable to other platforms as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The correct way to extract a jwt token from headers and integrate it into an express application

After successfully implementing both the frontend and backend in express.js with authentication and authorization using JWT, I have confirmed that the JWT token is being properly set upon login. You can see the auth-token key in the image below: https://i ...

Pop-up message on card selection using JavaScript, CSS, and PHP

I have a total of 6 cards displayed in my HTML. Each card, when clicked, should trigger a modal window to pop up (with additional information corresponding to that specific card). After spending a day searching for a solution online, I've come here s ...

Using Regular Expression in JavaScript to replace variables within a string

Looking for assistance to replace keywords in a string with pre-defined variables. Currently, the code only displays variable names instead of their content. Can anyone provide help? Desired Output: 1111Test1 2222Test2 3333Test3 Current Output ...

Can the tooltip of an element be changed while the old tooltip is still visible without having to move the mouse away and then back again?

When I have an HTML anchor element with a tooltip that appears when the mouse is hovered over it, and then I use JavaScript to change the tooltip's text using element.title = "Another Tooltip", the new tooltip text does not immediately update visually ...

Unable to display canvas background image upon webpage loading

Currently working on a JavaScript project to create a captcha display on a canvas. The issue I'm facing is that the background image does not load when the page initially opens. However, upon hitting the refresh button, it functions as intended. Here& ...

Encountering Issue When Initiating Internet Explorer through Selenium

My current challenge involves attempting to launch Internet Explorer using Selenium with the provided code snippet: import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; imp ...

The act of copying and pasting on Android involves a special character that is copied to the clipboard along

In my app, I have enabled the copy paste functionality on a textview. The textview contains HTML content which is set using Html.fromHtml. However, I have observed that when copying multiline HTML where a line starts with a link (<a> tag), it adds a ...

Getting a precise element from an array in JSON using Angular 5

I've been struggling to fetch a specific value ("isRight") from a JSON object. I have tried multiple solutions found on stackoverflow and even compared my code with one of them, but I keep getting the value as undefined without any errors. { ...

Is it possible to determine the time format preference of the user's device in Angular? For example, whether they use a 24-hour system or a 12-hour system with AM

In Angular, is there a way to determine whether the user's time format is set to 24-hour or 12-hour system? Any help would be greatly appreciated. Thanks! ...

The compatibility of Datatables responsive feature with ajax calls appears to be limited

I recently started using the datatables plugin and have encountered an issue with responsive tables. While I successfully implemented a responsive table and an AJAX call on a previous page, I am facing difficulties with it on a new page for unknown reasons ...

What are the best practices for correctly implementing Angularjs ng-repeat?

As a newcomer to angularjs, I am in the process of building an app where the output is already set up correctly. However, I am looking to achieve the same without relying on jQuery, and instead want to utilize the angularjs method using "ng-repeat". Below ...

React with Typescript - Type discrepancies found in Third Party Library

Recently, I encountered a scenario where I had a third-party library exporting a React Component in a certain way: // Code from the third party library that I cannot alter export default class MyIcon extends React.Component { ... }; MyIcon.propTypes = { ...

Looking to trigger the closing of a q-popup-proxy by clicking a button from a separate component

I am currently working with a q-popup-proxy component in my project. <q-btn label="Add Fault" class="addFaultButton" dense @click="openPopUp()"> <q-popup-proxy position="center" v-if="openFaults" ...

Sharepoint iframe resize issue causing postMessage to not work properly

I've been struggling to resize an iframe within a Sharepoint environment using Cross Domain. Despite trying numerous solutions found online, the only one that seems to work is Baker Humadi's postMessage solution mentioned towards the end: To imp ...

Navigating through various tables in a Java Selenium program and iterating through each one

I have a complex table structure where rows are nested inside tables within other tables. It may sound confusing, so let me break it down for you: .//*[@id='chatComponent']/div/swx-navigation /div/div/div/div[2]/div[1]/div/ul/li[*]/div/ul/li[*] ...

Performing an XMLHttpRequest in the same JSP file using Javascript

I am working on a JSP file that contains three dropdown boxes labeled "countries", "regions", and "cities". My goal is to populate the regions based on the selected country, and the cities based on the selected region. I have managed to achieve this using ...

Downloading Files through AngularJS on the Front End

When I click the download button next to the file name, I am retrieving the file content from the backend. The content is in bytes and the file can be of any type. How do I convert the data received from the backend into a file and automatically download ...

Generating intricate JSON structures with JavaScript programming instructions

In the world of JSON, I am still a novice. I need to use JavaScript to construct the JSON structure below, but I'm struggling with adding the second element ("12101") and populating the people in the JSON Structure. Below is the code I tried, however, ...

Is there a way to modify the appearance of a particular element in ng-repeat?

On my website, I am using ng-repeat to display various dish postings. I want to trigger a modal to open when I click on a dish, showing the specific data for that dish. To achieve this, I'm assigning each dish a modal div with a unique ID (dish-$index ...

Can you provide instructions on executing package dependencies using yarn in the command line? For example, is there a command similar to npx tsc init for initializing a npm

When utilizing yarn, the node_modules folder is not present. Instead, dependencies are stored in a .yarn/cache folder. I attempted to use yarn dlx tsc init and npx tsc init, but they did not achieve the desired result. There are various development depend ...