主页 > 未分类 > javascript与android交互,Webview addJavascriptInterface()方法

javascript与android交互,Webview addJavascriptInterface()方法

通过该方法,可以使网页内容和应用程序进行交互,实现更多的操作特性。如下为一个简单示例,详细代码见附件。

package com.example.jstest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

public class JsWebDemo extends Activity {  
    /** Called when the activity is first created. */  
 WebView wView;
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);    
        wView = (WebView)findViewById(R.id.wv1);  
           
        ProxyBridge pBridge = new ProxyBridge();  
        wView.addJavascriptInterface(pBridge, "AliansBridge");  
           
        WebSettings wSet = wView.getSettings();  
        wSet.setJavaScriptEnabled(true);  
           
        wView.loadUrl("file:///android_asset/index.html");  
           
    }  
 
    public class ProxyBridge {  
        public String one () {
          wView.loadUrl("http://www.baidu.com");
          Toast.makeText(getApplicationContext(),"xdtianyu",Toast.LENGTH_LONG).show();
            return "xdtianyu";  
        }  
    }  
 
}

jstest

发表评论

邮箱地址不会被公开。 必填项已用*标注