phonegap plugin with callback context (CallbackContext) to trigger an update from native java to phonehap javascript several times

Posted: February 1, 2013 in android, javascript, jquery mobile, mobile, phonegap, snippets

This post continues previous post where I wanted that after the user is done in the native activity he suppose to press the back button and the phonegap activity should understand that the native activity has finished and to try and grab a number from the native code from with javascript with the phonegap js interface. The final result is that phonegap saves in database the result of the native code.

My conclusion in the previous post was that  I will create me a phonegap plugin and use CallbackContext to update the phonegap code with new data.

found some links about phonegap plugins:

I guess i will follow the official tutorials from phonegap website.

I documented the process of first coping with the phonegap plugin in this video post: this is very basic i guess and u can do it on your own.

well, ofcourse I had to remove the brackets in the config.xml in the plugin value.

and i change the javascript code to call class MyPhonegapInterface instead of Echo.

window.echo = function(str, callback) {
cordova.exec(callback, function(err) {
callback(‘Nothing to echo.’);
}, “MyPhonegapInterface”, “echo”, [str]);
};

ok so now i’m getting the following error

02-01 12:56:41.002: E/Web Console(5392): Uncaught TypeError: Object #<an Object> has no method ‘exec’ at file:///android_asset/www/workouts.html:90

I triggering the javascript function like this:

$(function(){

//loadWorkouts();

window.echo = function(str, callback) {
cordova.exec(callback, function(err) {
callback(‘Nothing to echo.’);
}, “MyPhonegapInterface”, “echo”, [str]);
};

window.echo(“echome”, function(echoValue) {
alert(echoValue == “echome”); // should alert true.
});
});

i guess that the cordove variable wasn’t declare yet. so i’ll try the deviceready event

function onDeviceReady() {
window.echo = function(str, callback) {
cordova.exec(callback, function(err) {
callback(‘Nothing to echo.’);
}, “MyPhonegapInterface”, “echo”, [str]);
};

window.echo(“echome”, function(echoValue) {
alert(echoValue == “echome”); // should alert true.
});
}

and great!! it works! got on my phone an alert with TRUE written inside 🙂

now i can continue and try to use callback context (CallbackContext) to trigger an update from native java to phonehap javascript several times (not just once).

ok. so my conclusion for how to use the CallbackContext several times as an update mechanism is to create a PluginResult and use it like this:

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, message);

pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);

pluginResult.setKeepCallback(false);
callbackContext.sendPluginResult(pluginResult);

so the phonegap knows to keep the pointer to the callback success function alive so i can use it to update the database using phonegap sqlLite.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s