In PhoneGap, JavaScript in the Android WebView runs on the main thread along with where the Java execute method runs. This sometimes causes blocking issues in the application. PhoneGap has some options for running on a separate thread. We just need to choose the best way to implement the multi-threading based on the way our plugin works.
If our plugin is interacting with the UI, then in our native Java file (the plugin file), we need to run it directly on the native UI thread such as follows after checking the action
// If our native code is interacting with the UI, then make it run on the main UI thread. this.cordova.getActivity().runOnUiThread(new Runnable() { public void run() { // Here goes our custom code callbackContext.success(); } }
If our plugin is not interacting with the UI, but is doing some heavy task then in our native Java file (the plugin file), we need to run it on separate thread such as follows
// If our native code is not interacting with the UI, then make it run on a separate thread. this.cordova.getThreadPool().execute (new Runnable() { public void run() { // Here goes our custom code callbackContext.success(); } }
That’s it. Now the plugin will run on other thread not affect the main UI thread.