1

I'm using this code to call an Android Activity using HTML button. But it is not working for me. I just want to call my Android Activity "echos.class" when I click the Button from my html page.

my JavaScriptInterface class

public class JavaScriptInterface {
    Context mContext;

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


    /** Show a toast from the web page */
    public void showToast(String toast) {
        Intent mainIntent = new Intent(mContext, echos.class); 
        JavaScriptInterface.this.startActivity(mainIntent); 


    }


    private void startActivity(Intent mainIntent) {
        // TODO Auto-generated method stub

    }
}

my webview class

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.web1);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        myWebView.loadUrl("file:///android_asset/www/index.html");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

my html page

<html>
<head>

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

    <script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>
</body>
</html>
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
user1635137
  • 25
  • 1
  • 6
  • Your method startActivity in interface JavaScriptInterface is empty. Start activity mContext.startActivity(mainIntent); – Pasha Aug 30 '12 at 07:17

1 Answers1

2

change the

JavaScriptInterface.this.startActivity(mainIntent); 

to

 mContext.startActivity(mainIntent); 

as JavaScriptInterface is not an activity

Athul Harikumar
  • 2,501
  • 18
  • 16