Android簡短Webview範例
private WebView mWebView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "new WebView(this)");
mWebView = new WebView(this);
WebSettings webViewSettings = mWebView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
// packages/apps/Browser/src/com/android/browser/BrowserSettings.java
webViewSettings.setAllowContentAccess(false);
webViewSettings.setAppCacheEnabled(true);
webViewSettings.setDatabaseEnabled(true);
webViewSettings.setDomStorageEnabled(true);
mWebView.setWebViewClient(mWebViewClient);
mWebView.setWebChromeClient(mWebChromeClient);
//String target_url = "http://tw.yahoo.com";
//String target_url = "http://www.youtube.com";
//String target_url = "https://www.youtube.com/embed/QjMBw2bTDLQ";
String target_url = "https://www.youtube.com/embed/1OF3icemRJc";
Log.i(TAG, "loadUrl:" + target_url);
mWebView.loadUrl(target_url);
//String playVideo= "<html><body>Youtube video .. <br><iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"https://www.youtube.com/embed/1OF3icemRJc\" frameborder=\"0\"></body></html>";
//mWebView.loadData(playVideo, "text/html", "utf-8");
setContentView(mWebView);
}
WebViewClient mWebViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
WebChromeClient mWebChromeClient = new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
if ((title != null) && (title.trim().length() != 0)) {
setTitle(title);
}
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
String message = "Console: " + consoleMessage.message() + " "
+ consoleMessage.sourceId() + ":"
+ consoleMessage.lineNumber();
Log.i(TAG, "onConsoleMessage " + message);
return true;
}
};
留言