going between native and phonegap activities in the same android project

Posted: January 28, 2013 in javascript, mobile, phonegap

I have a native activity that captures audio. I had to go native on that one because of realtime issues.

So no I have my fancy html5 phonegap app and i need to trigger the native activity.

I encountered on that post:

How to call an Android Activity from PhoneGap

and try to implement the first answer.

on the native code I just copied this

appView.addJavascriptInterface(this, “MainActivity”);

without this code

super.init(); // Calling this is necessary to make this work

and the activity crashed.

when i included that line everything worked great.

 

returning values from native functions to phonegap javascript code:

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.init(); // Calling this is necessary to make this work
appView.addJavascriptInterface(this, “MainActivity”);
super.loadUrl(“file:///android_asset/www/index.html”);
}

public int testReturned ()
{
return 42;
}

and from the phonegap js side:

alert (window.MainActivity.testReturned());

 

and it worked… 🙂

Advertisements
Comments
  1. FDB says:

    in new versions you need to put ‘@JavascriptInterface’ above your functions:

    @JavascriptInterface
    public int testReturned ()
    {
    return 42;
    }

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