Using Javascript in Android application

Last Updated: Jan 17, 2016 10:30PM PST

In order to use Apsalar in JavaScript code that is part of your Android application, insert the following into your application code for your main activity:

ApsalarJSInterface ApsalarJSInterfaceInstance = new ApsalarJSInterface(this);
ApsalarJSInterfaceInstance.setWebViewId(R.id.webview);
myWebView.addJavascriptInterface(ApsalarJSInterfaceInstance, "ApsalarInterface"); 

where myWebView is the name of your webview. Note that you will need to do this for each unique webview that you may have in your application.

This code should be placed into the same method as your Apsalar.startSession call. It is recommended that the code be placed into the onCreate method of your application. An example of this code is:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView myWebView = (WebView) this.findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    Apsalar.startSession(this, "myApiName", "mySecret");
    ApsalarJSInterface ApsalarJSInterfaceInstance = new ApsalarJSInterface(this);
    // set your main web view URL
    myWebView.loadUrl("file:///android_asset/index.html");
    //R.id.webview below is an int generated by the android system, can be found in the R file.
    ApsalarJSInterfaceInstance.setWebViewId(R.id.webview);
    myWebView.addJavascriptInterface(ApsalarJSInterfaceInstance, "ApsalarInterface");
}

 

You can then use the following JS calls similar to those described above for Java:

ApsalarInterface.event(eventName)
ApsalarInterface.event(eventName, eventArgsJSON)
ApsalarInterface.endSession()
where eventName and eventsArgsJSON is a string.

For example:

ApsalarInterface.event('event');
ApsalarInterface.event('test', '{"a1":"bar", "a2":"boo", "a3":"baz"}');

You CANNOT use calls to Apsalar.startSession or Apsalar.reStartSession from JavaScript. These must be done from Java as described above.

support@apsalar.com
http://assets0.desk.com/
apsalarinc
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
Invalid characters found
/customer/en/portal/articles/autocomplete