[英]How to start new activity fron android webview javascript interface
我正在开发android Web应用程序。
我想从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();
}
}
该代码工作正常。 但是当我尝试开始新活动时,什么都没有发生。这是我的代码(对不起,英语不好)
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);
}
}
尝试这个:
在您的JavaScript代码中
<script type="text/javascript">
function moveToScreen() {
Android.moveToNextScreen();
}
</script>
在您的Java代码中:
public void moveToNextScreen(){
//Move to Next screen
Intent newintent = new Intent(FirstActivity.this, SecondIntent.class);
startActivity(newintent);
}
将它们添加到您的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.