简体   繁体   English

如何开始新活动从Android WebView JavaScript界面

[英]How to start new activity fron android webview javascript interface

I'm working on android web application. 我正在开发android Web应用程序。

I want to start new activity from javascript.I googled and found a code to show toast.this is it. 我想从javascript开始新活动。我用google搜索并找到了显示toast.this的代码。

    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();
    }
}

That code is working fine. 该代码工作正常。 but when I try to start new activity nothing happen.here is my code(sorry for bad english) 但是当我尝试开始新活动时,什么都没有发生。这是我的代码(对不起,英语不好)

    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) {
        Intent showAd = new Intent(getBaseContext(), ShowAd.class);
        startActivity(showAd);
    }
}

Try this: 尝试这个:

In your javascript code 在您的JavaScript代码中

<script type="text/javascript">
 function moveToScreen() {
        Android.moveToNextScreen();
    }
</script>

In your java code: 在您的Java代码中:

public void moveToNextScreen(){

                             //Move to Next screen
                             Intent newintent = new Intent(FirstActivity.this, SecondIntent.class);  
                             startActivity(newintent);  

    }

Add these in your onCreate 将它们添加到您的onCreate中

webview.getSettings().setJavaScriptEnabled(true);

        webview.addJavascriptInterface(new WebAppInterface(this), "Android");
        //Load URL inside WebView
        webview.loadUrl("Your html page url");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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