简体   繁体   中英

Toast does not work in webview

Here is my code, this is literally step by step from developer.android.com

and it just doesn't work, no matter how many times I run it.

public class MainActivity extends Activity {

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


        WebView myWebView = (WebView) findViewById(R.id.webview);


        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);


        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        myWebView.loadUrl("http://www.google.com");



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }

}

When I run my app, the webpage will load, toast never shows.

I just can't seem to find the problem. Can someone tell me if this works for them?

EDIT: Here is some more that I'm confused about. Right after this, the instructions are

This creates an interface called Android for JavaScript running in the WebView. At this point, your web application has access to the WebAppInterface class. For example, here's some HTML and JavaScript that creates a toast message using the new interface when the user clicks a button:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

I have no idea where that part of js code goes either..

I have no idea where that part of js code goes either..

Create an html page in your assets folder, let's say named myWonderfulWebPage.html .

Copy the following HTML code to it :

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>

As you can see, when you'll click the button, the function showAndroidToast will be called and this function will call the one you defined in your Java code.

Now go back in your activity and load this page into your webview :

myWebView.loadUrl("file:///android_asset/myWonderfulWebPage.html");

Now you will see that it shows a blank page, with a button. Click it and it will show you the Toast on your webpage.

If the code you show is what is actually used then of course it will not work.

myWebView.loadUrl("http://www.google.com");

Google.com does not invoke the JS function you have added a bridge to. You need to use your own webpage that does indeed invoke the function, either local or on the web.-.-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM