0

I have searched high and low and found so many examples of this, but unable to get it to work, my setup currently is:

Notifier.java

public class Notifier{  
    Context mContext;

    Notifier(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void showText()  
    {   
        Toast.makeText(mContext, "Some text!", Toast.LENGTH_LONG).show();
    }
}

SearchLicenseActivity.java

public class SearchLicenseActivity extends Activity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_license);

        LoadSearch();
    }

    public void LoadSearch(){
        webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);   
        webView.addJavascriptInterface(new Notifier(this), "Android");
        webView.loadUrl("javascript:Android.showText();");
    }
}

So I am expecting a Toast to show. It may be worth noting that this SearchActivity gets created when a button on a previous Activity is clicked; so I want it to execute LoadSearch straight away and get the Toast from the JavaScript.

I hope one of you out there can cure my woes over this!

Edit: I am also not getting any errors in LogCat.

loufar
  • 37
  • 8

1 Answers1

0

OK, it turns out webView must have a webpage loaded.

Even if the url is "about:blank" this seems to work. So...

webView.loadUrl("about:blank");
webView.loadUrl("javascript:Android.showText();");

...works.

loufar
  • 37
  • 8