5

Is is possible to call main activity's methods from the JavaScript interface of a WebView object? Or to access SharedPreferences from this interface so I could read the data with my activity? I would like to let my Activity know that a specific JavaScript action occured.

Krzychu
  • 241
  • 2
  • 4
  • 12
  • possible duplicate of [Adding JavaScript Interface to a Browser](http://stackoverflow.com/questions/3765860/adding-javascript-interface-to-a-browser) – skolima Oct 19 '12 at 17:45

3 Answers3

2

Your activity:

/*
 * entry point of the application starting the index.html of PhoneGap
 * */

package com.test.my.myApp;

import org.apache.cordova.DroidGap;

import android.os.Bundle;
import android.util.Log;

import com.google.analytics.tracking.android.EasyTracker;

public class MyOpelMainActivitiy extends DroidGap
{

    String curPhoneNumber;

    @Override
    public void onCreate(Bundle savedInstanceState)
    { 
        super.onCreate(savedInstanceState);
        super.setIntegerProperty("splashscreen", R.drawable.splash);
        super.loadUrl("file:///android_asset/www/app/index.html",15000); 

    }
    @Override
    public void onStart() {
      super.onStart();
      this.appView.addJavascriptInterface(new JsInterface(), "android");  
      curPhoneNumber = "test";

      // The rest of your onStart() code.
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onDestroy() 
    {
        super.onDestroy();       
        com.google.analytics.tracking.android.EasyTracker.getInstance().activityStop(this);
    }

    public class JsInterface{   





            public String getPhoneNumber()
            {

                // Here call any of the public activity methods....
                return curPhoneNumber;
            }
        }
    }

HTMl markup:

<script type="text/javascript">
            alert(android.getPhoneNumber());
        </script>

========================================================================

Sheetal
  • 1,368
  • 1
  • 9
  • 15
  • In this example for api17+ the getPhoneNumber must have the annotation @android.webkit.JavascriptInterface – MazarD Jun 20 '17 at 16:42
2

Yes, two way communication between JavaScript and your application is possible through WebView.addJavascriptInterface(). Check this example:

http://android-developers.blogspot.com/2008/09/using-webviews.html

Caner
  • 57,267
  • 35
  • 174
  • 180
  • OK, this sort of solves my problem. In the example given by you it works, because DemoJavaScriptInterface is a subclass of WebViewDemo class and can access its methods. But is there a way to achieve it when it is not a subclass? – Krzychu Oct 05 '11 at 12:33
  • It doesn't use any of `WebViewDemo` methods. It only uses `mHandler` and that could be passed to the constructor. – Caner Oct 05 '11 at 12:42
  • I tried to pass the `mHandler` but the main Activity is still inaccessible. When I try `mHandler.post(new Runnable() { public void run() { mWebView.loadUrl("javascript:wave()"); } }); ` I get _myWebView cannot be resolved_ – Krzychu Oct 05 '11 at 13:00
  • You also need to pass `mWebView` to cunstructor, I didn't see that before. – Caner Oct 05 '11 at 13:01
  • Actually I don't. I found out that myWebView was private and that's why I wasn't able to access it :) Nevertheless I think I will make a subclass (like in the example) because it seems much easier without handlers. Thanks for your help! – Krzychu Oct 05 '11 at 13:12
1

You can use WebView.addJavascriptInterface function to achieve this. More info: In Google documentation

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • 1
    Yes, I am using addJavascriptInterface(new JavaScriptInterface(this), "Android"); The question was: how to call main activity's method from within the JavaScriptInterface class. – Krzychu Oct 05 '11 at 12:30
  • Can you update your question with the code you have at the moment? – SERPRO Oct 05 '11 at 12:47