Phonegap(android) open a native java UI Activity while updating phonegap database with results from that native activity

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

ok. so in the previous posts i:

  1. opened a native java UI Activity – see post
  2. created a plugin that uses CallbackContext and updates the phonegap database – see post

I want that every several seconds the database will be updated (in case of a crash, i want the data to be valid) so now I will connect the two.

The native Activity is going to trigger the callback function every some and some seconds so database is continuously updated.

 

so on the javascript side when user hits the “new” button:

function startWorkout ()
{
gCurrentWorkoutTimestamp = Number(Date.now())/1000;
// add a new item to database with the new workout
gWorkoutsTable.addWorkout({“date”: gCurrentWorkoutTimestamp, “duration”: 0});

// set a callback functionallity from native to phonegap to update database constantly during workout
window.registerWorkoutUpdate(
function(duration) {
gWorkoutsTable.updateWorkout({“date”: gCurrentWorkoutTimestamp, “duration”: duration});
loadWorkouts();
}
);

// now start native java activity
window.MainActivity.phonegapInterfaceLaunchTherapyUtility();
}

you can see that on a new workout, i’m starting a new timestamp (same as ID), adding a new sql INSERT statement and create the item in the database, registering in the phonegap plugin to get updates (on every update trigger an UPDATE sql statement with a new value) and finally – start the native UI java activity with phonegap js interface (same way i describe on this post).

 

Everything works just fine! 🙂

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