2

I think I'm missing something here. I'm used to sending data from javascript to java and back with calls to execute and back with the callbackContext methods.

But if at some points, lets say I have a running thread that needs to send data to the javascript at regular intervals, how should I do that then ? (This assumes that this task is running and has not been triggered by a javascript action, thus no callbackContext is available)

Sephy
  • 50,022
  • 30
  • 123
  • 131

2 Answers2

3

You can always execute javascript from java doing this:

String js = "alert('test')";
webView.loadUrlNow("javascript:" + js);

Or you can init the plugin and keep the callback doing this

PluginResult pgRes = new PluginResult(PluginResult.Status.OK, "message");
pgRes.setKeepCallback(true);
callbackContext.sendPluginResult(pgRes);

Added example provided by Sephy

private String myCbkId;
// Store callbackId from a call to execute 
@Override public boolean execute(String action, JSONArray arr, CallbackContext cbkCtx) throws JSONException { 

    myCbkId = cbkCtx.getCallbackId(); 
    JSONObject data = arr.getJSONObject(0); 
    String ack = data.getString("data"); // You can acknowledge to the callback for instance and keep it alive 
    Log.d(TAG, "ack".equals(ack) ? "ack !" : "not ack !");

    // These lines can be reused anywhere in your app to send data to the javascript
    PluginResult result = new PluginResult(PluginResult.Status.OK, ack);
    result.setKeepCallback(true);//This is the important part that allows executing the callback more than once, change to false if you want the callbacks to stop firing  
    this.webView.sendPluginResult(result, this.myCbkId); 

    return true; 
}
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
  • 1
    Yeah that's kinda what I ended doing but this needs some explaining and more details for people who come accross this answer to get the point. I'll publish my solution later to compare it to yours. – Sephy Dec 01 '14 at 13:10
  • Which one needs more explanation? first one is trivial, is the way you execute javascript from java. Second part maybe needs some explanation, but your question was vague too, it seemed you just needed orientation, you didn't ask for a complete example – jcesarmobile Dec 01 '14 at 13:32
  • Here is my complete solution. If it's ok with you to publish it in your answer as a more detailled version, I'll check your answer. Thanks for the pointer ! – Sephy Dec 01 '14 at 20:37
  • // Store callbackId from a call to execute @Override public boolean execute(String action, JSONArray arr, CallbackContext cbkCtx) throws JSONException { myCbkId = cbkCtx.getCallbackId(); JSONObject data = arr.getJSONObject(0); String ack = data.getString("data"); // You can acknowledge to the callback for instance and keep it alive Log.d(TAG, "ack".equals(ack) ? "ack !" : "not ack !"); – Sephy Dec 01 '14 at 20:43
  • // These lines can be reused anywhere in your app to send data to the javascript PluginResult result = new PluginResult(PluginResult.Status.OK, ack); result.setKeepCallback(true); this.webView.sendPluginResult(result, this.myCbkId); return true; } – Sephy Dec 01 '14 at 20:43
  • np. Hope this will help people passing by. – Sephy Dec 02 '14 at 08:40
  • how do you receive the pluginResult "message" on the cordova / javascript side? – nomaam Jan 19 '21 at 17:51
  • when you call the plugin function from js, one of the params is the callback, the function you pass as callback will be executed every time the native code is executed – jcesarmobile Jan 19 '21 at 18:52
0

If what you need is communication from server to client in asynchronous way than websockets are the solution for you. Exact implementation depends on which web server you use so you'd net to give more details but they all work same way more or less.

Łukasz Zwierko
  • 621
  • 7
  • 15
  • I'm not sure you got my issue well, I'm trying to talk between my java plugin and my web application wrapped with cordova...I'm not trying to do HTTP/WS communication. – Sephy Nov 30 '14 at 23:21
  • Right sorry. Did you see http://stackoverflow.com/questions/15852499/javascript-calls-from-android-using-phonegap – Łukasz Zwierko Nov 30 '14 at 23:26
  • Didn't see this one before you mention it, thx. But 1) it's a bi outdated as off Cordova 3, some things changed with plugins. 2) This question is not really clear to me and does not provide a clear and generic solution to reuse when you want to communicate from java to js with Cordova/Phonegap. If I understand it well, it mainly explains how to communicate from js to java, which I already know how to do. – Sephy Nov 30 '14 at 23:37