3

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,

Breed Hansen
  • 1,159
  • 5
  • 13
  • 21

3 Answers3

9

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");
1

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";
             }
     }      
temple
  • 964
  • 1
  • 12
  • 21
0

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

osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • Thanks for answer. I know PhoneGap and Cordova. I want to develop native app. – Breed Hansen Feb 28 '13 at 09:17
  • Cordova Plugin just provides you a communication between web view and native side, so it doesn't mean that you cannot have native user interface, you can make it 100% native. – osayilgan Feb 28 '13 at 09:20