Reputation: 231
I am new to Android and was trying to setup the Android Webview to load a javascript function and return the result to a callback function.
See the code below
public class AWebViewActivity extends Activity {
/**
* Defines an Interface to call Syntax Highlighting Javascript and return the result
* to a string by calling the call back function.
*/
public class JavaScriptInterface {
Context mContext;
JavaScriptInterface(Context c) {
mContext = c;
}
//add other interface methods to be called from JavaScript
// This annotation is required in Jelly Bean and later:
@JavascriptInterface
public void receiveValueFromJs(String str) {
//do something useful with str
Toast.makeText(mContext, "Received Value from JS: " + str,Toast.LENGTH_LONG).show();
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Try WebView
WebView webView = (WebView) findViewById(R.id.editor_view);
//webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
// RiNxX's suggestion
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url){
view.loadUrl("javascript:getValue()");
}
});
webView.addJavascriptInterface(new JavaScriptInterface(this), "MyAndroid");
webView.loadUrl("file:///mnt/sdcard/a.html");
//SystemClock.sleep(1000);
//webView.loadUrl("javascript:getValue()");
}
}
The HTML file is given below:
<html>
<head>
</head>
<body>
<script type="text/javascript">
function getValue()
{
var val="Amith Raravi";
MyAndroid.receiveValueFromJs(val);
}
</script>
Hey ya
</body>
</html>
I have also added the INTERNET permission as a child of the Manifest element. I went through the other questions posted on StackOverflow and moved the script to Body of the HTML.
After i added the changes suggested by RiNxX, the app closes(it doesnt execute the callback) and i get the below warning in LogCat
JNI WARNING: jarray 0x405414e8 points to non-array object (Ljava/lang/String;)
I/dalvikvm(361): "WebViewCoreThread" prio=5 tid=9 NATIVE
I/dalvikvm(361): | group="main" sCount=0 dsCount=0 obj=0x4051d580 self=0x29a1a8
I/dalvikvm(361): | sysTid=370 nice=0 sched=0/0 cgrp=default handle=2728672
I/dalvikvm(361): | schedstat=( 277421205 460097188 88 )
I/dalvikvm(361): at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
I/dalvikvm(361): at android.webkit.BrowserFrame.stringByEvaluatingJavaScriptFromString(Native Method)
I/dalvikvm(361): at android.webkit.BrowserFrame.loadUrl(BrowserFrame.java:246)
I/dalvikvm(361): at android.webkit.WebViewCore.loadUrl(WebViewCore.java:1570)
I/dalvikvm(361): at android.webkit.WebViewCore.access$1400(WebViewCore.java:53)
I/dalvikvm(361): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:956)
I/dalvikvm(361): at android.os.Handler.dispatchMessage(Handler.java:99)
I/dalvikvm(361): at android.os.Looper.loop(Looper.java:123)
I/dalvikvm(361): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
I/dalvikvm(361): at java.lang.Thread.run(Thread.java:1019)
E/dalvikvm(361): VM aborting
I have been at it for over a day now. Any help would be most welcome:)
Upvotes: 2
Views: 9065
Reputation: 20589
I'm having same issue, but worked fine when I try to load the file from assets:
webView.loadUrl("file:///android_asset/a.html");
Upvotes: 2
Reputation: 5045
java script Called from android webview
webView.loadUrl("javascript:setDatePickervalue(\""
+ pselSelectedDateProp.getFormatedDate() + "\")");
Java Script Function
<script type="text/javascript" language="JavaScript">
function setDatePickervalue(selecteddate){
document.getElementById("text").innerHTML = selecteddate.toString();
//alert('hello' + selecteddate.toString());
}
</script>
.... i was trying to implement same thing like u did... its giving me vm aborting exception in emulator...BUT ITS WORKING FINE ON DEVICE..
Upvotes: 0
Reputation: 231
Apparently, this has been a long standing bug in the 2.3.3 emulator.
See the below link.
Currently downloading 2.2 and 3.0 emulators.
Thank You for all the help, and the great forum.
I will be back with more questions!
Upvotes: 5
Reputation: 2176
Add webview client that calls javascript interface when page is loaded
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:getValue()");
}
webview.loadUrl(SOME_WEBPAGE);
Upvotes: 1