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

calling javascript function from an android activity - Stack Overflow

programmeradmin2浏览0评论

I wan to call a javascript function from an android activity but it doesn't seem to work. I have used the android webview function webview.loadUrl("javascript:function()"); This is my android code:

package .example.web;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview;
    int i=30;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView)findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/index.html");
        webview.loadUrl("javascript:change(i)");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This is my html code:

 <html>
<body>
<div id="texta">text</div>
<script>

function change(num){
document.getElementById("texta").innerHTML=num;
}

</script>

</body>
</html>

I wan to call a javascript function from an android activity but it doesn't seem to work. I have used the android webview function webview.loadUrl("javascript:function()"); This is my android code:

package .example.web;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview;
    int i=30;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView)findViewById(R.id.webView1);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("file:///android_asset/index.html");
        webview.loadUrl("javascript:change(i)");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This is my html code:

 <html>
<body>
<div id="texta">text</div>
<script>

function change(num){
document.getElementById("texta").innerHTML=num;
}

</script>

</body>
</html>
Share Improve this question edited Mar 28, 2013 at 4:19 lakshman 2,7416 gold badges40 silver badges64 bronze badges asked Mar 28, 2013 at 3:02 user2206969user2206969 531 gold badge1 silver badge6 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 7

It's likely your page isn't fully loaded by the time you're trying to execute the javascript. Can you try:

webView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:change(i)");
    }
});

Try:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = (WebView)findViewById(R.id.webView1);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/index.html");
    webview.loadUrl("javascript:change("+String.valueOf(i)+")");

}

What is "i", If "i" is a number then Specify the Number, if "i" is a character then kindly enclose it in double Quotes, you are calling a method of some arguments, so kindly correct your mistake.

try webview.loadUrl("javascript:change(\""+i"\")");

 protected void onCreate(Bundle savedInstanceState) 
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   final Activity MyActivity = this;
   text=(EditText)findViewById(R.id.textValue);
   Show=(Button)findViewById(R.id.textButton);
   webView= (WebView) findViewById(R.id.webView);
   getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
                                    Window.PROGRESS_VISIBILITY_ON);
   webView.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {

   MyActivity .setTitle("Loading...");
     MyActivity .setProgress(progress * 100);

     if (progress == 100)
     MyActivity .setTitle("Android Dhina");
    }
   });
     webView.setWebViewClient(new WebViewClient());
     webView.addJavascriptInterface(new WebAppInterface(this), "Android");

     webView.getSettings().setJavaScriptEnabled(true);
     webView.loadUrl("file:///android_asset/web.html");

     Show.setOnClickListener(new OnClickListener()
      {
       @Override
       public void onClick(View v) 
       {
         webView.loadUrl("javascript:callFromAndroidActivity
                          (\""+text.getText().toString()+"\")"); }
        });

Try it:

String URL = "file:///android_asset/filename.html";
webviewBrowser=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient(webview.loadUrl(URL));

Try to change this :

webview.loadUrl("javascript:change(i)");

to this

webview.loadUrl("javascript:change(" + i +")");

the way you have write it the "i" is not the variable "i" from your java code but just a String named "i".

发布评论

评论列表(0)

  1. 暂无评论