Stephen
Stephen

Reputation: 10079

Html Button OnClick to move Another Activity in Android

I am getting None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17. compile error at addJavascriptInterface method.

I have pointed out the error line in below code:

WebView code:

import android.webkit.JavascriptInterface;

webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);

 if (new File(url).exists()) {
            webView.loadUrl(FILENAME_PREFIX + url);
            Log.d("fileurl", "" + FILENAME_PREFIX + url);

        }

        webView.addJavascriptInterface(new Object() { --->None of the methods in the added interface @android.webkit.JavascriptInterface.They will not be visible in Api level 17.

            @JavascriptInterface
            public void performClick() {
                Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intRef);
            }
        }, "ok");

Html Code:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>
    <body>

         <div>
            <button type="button" onclick="ok.performClick();">OK</button>
        </div>
  </body>
</html>

I am using minSdkVersion 11 and targetSdkVersion 22.I had called @JavascriptInterface in before method.You could see that in above code.

Anyone can help me with this.Thank you.

Upvotes: 2

Views: 2653

Answers (2)

Stephen
Stephen

Reputation: 10079

Html Page:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="style.css" />

        <script type="text/javascript">

        function moveToScreenTwo() {
        Android.moveToNextScreen();
        }
        </script>

    </head>

  <body>
     <div>
            <input type="button" value="Locate" onClick="moveToScreenTwo()" />
        </div>
  </body>
  </html>

FirstACtivity.java:

import android.webkit.JavascriptInterface;

webView = (WebView) findViewById(R.id.load_url);
webView.getSettings().setJavaScriptEnabled(true);

 if (new File(url).exists()) {
            webView.loadUrl(FILENAME_PREFIX + url);
            Log.d("fileurl", "" + FILENAME_PREFIX + url);
  webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        }
  //Class to be injected in Web page
    public class WebAppInterface {
        Context mContext;

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

        @JavascriptInterface
        public void moveToNextScreen() {

            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(i);

        }
     }

For more Reference: Check this tutorial.

Upvotes: 2

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

You will have to annotate each method with @JavascriptInterface within your class that you'd like to access from Javascript.

EDIT: I have implemented this way in my code:

webView.addJavascriptInterface(new WebAppInterface(this), "ok");
public class WebAppInterface {
        Context mContext;

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

        @JavascriptInterface
        public void performClick() {
            Intent intRef=new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(intRef);
        }
    }

I hope it helps!

Upvotes: 2

Related Questions