Reputation: 81
I'm working on android web application.
I want to start new activity from javascript.I googled and found a code to show toast.this is it.
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);
}
}
Upvotes: 1
Views: 2154
Reputation: 5261
Try this:
In your javascript code
<script type="text/javascript">
function moveToScreen() {
Android.moveToNextScreen();
}
</script>
In your java code:
public void moveToNextScreen(){
//Move to Next screen
Intent newintent = new Intent(FirstActivity.this, SecondIntent.class);
startActivity(newintent);
}
Add these in your onCreate
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(this), "Android");
//Load URL inside WebView
webview.loadUrl("Your html page url");
Upvotes: 2