1

I have included phonertc sample application as part of my Android Native Java Application. On my home activity, I have a button which start the phonertc sample app on click. Here is what button does:

Intent i = new Intent(this, CordovaApp.class);
startActivity(i);

Inside CordovaApp.class, I have the following code taken from the sample app of phonertc:

public class CordovaApp extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}

This loads the sample phonertc application in a separate activity. I want to send some data of my own from Java to sample phonertc application when it is loaded.

My goal is to do communication between the phonertc application and my java android code apart from the communication of the plugin. How can I do this?

Is there any way that phonertc application calls function of my java android code after getting loaded in webview?

Please help.

SamFast
  • 1,054
  • 2
  • 16
  • 31
  • You want to call a Java function from HTML/Javascript side in a hybrid app,right? If so, I'll explain. – Faruk Yazici Feb 26 '15 at 14:24
  • Yes, and other way round too. I know how to do in simple webview but can't understand this in case of cordova plus phonertc plugin – SamFast Feb 26 '15 at 14:26
  • I don't know about the phonertc case, but I can explain for example starting an Android intent on click of an HTML button within cordova app scope. but if you know that, never mind :) – Faruk Yazici Feb 26 '15 at 14:28
  • Ok, can you explain only cordova app scope then, I would do the phonertc myself – SamFast Feb 26 '15 at 14:31

1 Answers1

0

Whatever your wevbiew is named, use addJavascriptInterface method for that. It basically enables Java functions to be accessed by Javascript side. First parameter is normally an interface class but you can simply use the same activity.

cordova_webview.addJavascriptInterface(this, "mainHandler");

Assumnig that you want to run for example the following activity changing method:

public void changePage(){           
    Intent intent = new Intent(MainActivity.this, 
                SecondActivity.class);
    startActivity(intent); 
}

Now on the HTML page, you can access it via myHandler interface;

<button onclick="window.mainHandler.changePage()">
    Change Page
</button>

Aa you see Cordova has not any different thing from this simple Android structure.

Faruk Yazici
  • 2,344
  • 18
  • 38