最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Android : callback function from java script to java - Stack Overflow

programmeradmin0浏览0评论

I have created an application in which i am using webview and loading a simple static html page. I am calling a java script function from activity but i am unable to call a function from java script. I have tried few of the link , but it didn't work .
Javascript Callback function pass to Android
I cannot call an android function from javascript
Here is my code. Thank you in advance.


acitivity_main.xml

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call JavaScript Function"
        android:onClick="callJavaScript" />

<WebView
    android:id="@+id/myWebView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

index.html

<html>
<head>
<script type="text/javascript">
        function displayMessage(){
            document.getElementById('test1').innerHTML = 'This is from java script.';
            Android.returnResult();
        }
    </script>
</head>
<body>
    <h1 id="test1">Hello World</h1>
</body>
</html>

Main activity has following two functions .

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) this.findViewById(R.id.myWebView);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new JavaScriptHandler(this), "Android");
        myWebView.loadUrl("file:///android_asset/index.html");
    }

    public void callJavaScript(View view) {
        Log.v(null, "Calling java Script");
        myWebView.loadUrl("javascript:displayMessage()");
    }

This is the JavaScriptHandler class

public class JavaScriptHandler {
    Context mContext;

    public JavaScriptHandler(Context context) {
        mContext = context;
    }

    public void returnResult() {
        Log.v(null, "result received");
    }
}

I have created an application in which i am using webview and loading a simple static html page. I am calling a java script function from activity but i am unable to call a function from java script. I have tried few of the link , but it didn't work .
Javascript Callback function pass to Android
I cannot call an android function from javascript
Here is my code. Thank you in advance.


acitivity_main.xml

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Call JavaScript Function"
        android:onClick="callJavaScript" />

<WebView
    android:id="@+id/myWebView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

index.html

<html>
<head>
<script type="text/javascript">
        function displayMessage(){
            document.getElementById('test1').innerHTML = 'This is from java script.';
            Android.returnResult();
        }
    </script>
</head>
<body>
    <h1 id="test1">Hello World</h1>
</body>
</html>

Main activity has following two functions .

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView) this.findViewById(R.id.myWebView);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new JavaScriptHandler(this), "Android");
        myWebView.loadUrl("file:///android_asset/index.html");
    }

    public void callJavaScript(View view) {
        Log.v(null, "Calling java Script");
        myWebView.loadUrl("javascript:displayMessage()");
    }

This is the JavaScriptHandler class

public class JavaScriptHandler {
    Context mContext;

    public JavaScriptHandler(Context context) {
        mContext = context;
    }

    public void returnResult() {
        Log.v(null, "result received");
    }
}
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Nov 27, 2013 at 13:22 Rana Ranvijay SinghRana Ranvijay Singh 6,1653 gold badges40 silver badges55 bronze badges 2
  • Kindly add alert statements in your javascript method to see if you javascript method is getting called properly. Otherwise I dont see any problem in your implementation. – Arunkumar Commented Nov 27, 2013 at 13:54
  • Thank you for replying , i have added a line to change the html text when java script is getting called and it was working anyway i got the answer looks like i had to use the annotation. – Rana Ranvijay Singh Commented Nov 28, 2013 at 4:51
Add a ment  | 

1 Answer 1

Reset to default 5

Make it

@JavascriptInterface
public void returnResult() {
    Log.v(null, "result received");
}

This annotation allows exposing methods to JavaScript.

发布评论

评论列表(0)

  1. 暂无评论