javascript与android交互,Webview addJavascriptInterface()方法
2013 年 5 月 9 日 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";
}
}
}
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";
}
}
}
发表评论