开发者

WebView addJavascriptInterface - How to use?

开发者 https://www.devze.com 2023-02-20 22:47 出处:网络
I\'m trying to fire a Java function residing in a HTML document that is loaded in my webView via an ImageButton on my title bar within my Android activity.I followed the help docs but I\'m not getting

I'm trying to fire a Java function residing in a HTML document that is loaded in my webView via an ImageButton on my title bar within my Android activity. I followed the help docs but I'm not getting it.

Can someone please correct my script; it would be a big help and more to learn off of.

The function in the HTML doc (mWebView):

<script type="text/javascript">
    $(document).ready(function() {
        function ToC(){
            $.mobile.changePage("docs/ToC.html", "slideup");
        };
    });
</script>

...and the code in my .main activity (main.java):

public c开发者_如何学Pythonlass main extends Activity {

    WebView mWebView;
    private Handler mHandler = new Handler();

// -- Called when the activity is first created. --/
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
           if (savedInstanceState != null)
                    ((WebView)findViewById(R.id.webView1)).restoreState(savedInstanceState);

// -- Set up the WebView -->
        mWebView = (WebView) findViewById(R.id.webView1);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new myJavaScriptInterface(), "jsi");
        mWebView.setWebViewClient(new myWebViewClient());
        mWebView.loadUrl("file:///android_asset/www/index.html");

    final class myJavaScriptInterface {

        myJavaScriptInterface() {

        ImageButton imageButton = (ImageButton) findViewById(R.id.bookBtn);
        imageButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mHandler.post(new Runnable() {
                    public void run() {
                    mWebView.loadUrl("javascript:ToC()");
                    }
                });
                }
            });
        }
     }
 }


$.mobile.changePage("docs/ToC.html", "slideup");

Assuming this modifies window.location.href or something similar, it would appear that perhaps this does not trigger shouldOverrideUrlLoading(). See window.location.href javascript does not trigger shouldOverrideUrlLoading for more, including my suggested workaround.


This is a bit late, but note that myJavaScriptInterface() never gets called, so the button's OnClickListener is never set, so the button won't do anything.

A javascript interface is for communication from javascript into your app; its methods are only called where you explicitly call them in your javascript. In order to call myJavaScriptInterface(), you'd put somewhere in your javascript: jsi.myJavaScriptInterface();. Presumably you'd put it in a place where you'd want the button's handler to be set, like maybe in $(document).ready(). Personally, I'd just set the handler in the main code, without the interface, but your way probably works, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消