javascriptandroidphonertc

PhoneRTC: Send data from Java to Javascript on start


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.


Solution

  • 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.