Reputation: 1179
I am developing native application. My application have a WebView
. I want to log this WebView
's component's every action. Which button clicked, which image dragged. After that, I will log those things, I want to store in SQLite.
So, I googled and find WebAppInterface
which communicates Java & JavaScript. But this communication isn't enough. I can't send database from WebAppInterface
class.
I want to ask this question actually (minute 46:00
).
Is it possible? Or, another solution?
Regards,
Upvotes: 3
Views: 11975
Reputation: 5827
You should add javascript interface to your webView like that:
webView.addJavascriptInterface(new JavascriptInterface(this), "AndroidFunction");
You should create interface class:
public class JavascriptInterface{
Context mContext;
JavascriptInterface(Context c) {
mContext = c;
}
public void save(String action){
// save to database
}
}
On the website you should call:
AndroidFunction.save("actionName");
Upvotes: 9
Reputation: 964
official link :http://developer.android.com/reference/android/webkit/WebView.html
class JsObject {
@JavascriptInterface
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
------------------------------- MUST API>17
my tune up, HTML, jsobj.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello TEST JS</title>
</head>
<body style="background: white; font-family: Helvetica">
<script type="text/javascript">
document.write(AndroidFunction.show());
AndroidFunction.save("abc");
</script>
</html>
Android:
web.addJavascriptInterface(new JsObject(), "AndroidFunction");
...
class JsObject{
@JavascriptInterface
public void save(String action){
L.d("action:"+action);
}
public String show(){
return "Hello Android";
}
}
Upvotes: 1
Reputation: 5893
Have you ever take a look on PhoneGap and Cordova Plugin ? Cordova Plugin provides really strong communication between Java to webview and webview to Java. And there are already some open source plugins written about logging the data, you can just embed into your application.
Take a look on this link : Cordova Plugin
Upvotes: 0