将网页嵌入到android应用中
2014-03-20 14:03
316 查看
将网页嵌入到android应用中
[java] view
plaincopy
package com.android.webviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView myWebView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取webview控件
myWebView = (WebView) findViewById(R.id.webview);
//加载服务器上的页面
myWebView.loadUrl("http://www.baidu.com");
//加载本地中的html
//myWebView.loadUrl("file:///android_asset/www/test2.html");
//加上下面这段代码可以使网页中的链接不以浏览器的方式打开
myWebView.setWebViewClient(new WebViewClient());
//得到webview设置
WebSettings webSettings = myWebView.getSettings();
//允许使用javascript
webSettings.setJavaScriptEnabled(true);
//将WebAppInterface于javascript绑定
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// return true;
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
[java] view
plaincopy
package com.android.webviewtest;
import android.content.Context;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
html代码
[html] view
plaincopy
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
这里调用android中的方法
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
[java] view
plaincopy
package com.android.webviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView myWebView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取webview控件
myWebView = (WebView) findViewById(R.id.webview);
//加载服务器上的页面
myWebView.loadUrl("http://www.baidu.com");
//加载本地中的html
//myWebView.loadUrl("file:///android_asset/www/test2.html");
//加上下面这段代码可以使网页中的链接不以浏览器的方式打开
myWebView.setWebViewClient(new WebViewClient());
//得到webview设置
WebSettings webSettings = myWebView.getSettings();
//允许使用javascript
webSettings.setJavaScriptEnabled(true);
//将WebAppInterface于javascript绑定
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// return true;
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
}
[java] view
plaincopy
package com.android.webviewtest;
import android.content.Context;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
html代码
[html] view
plaincopy
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
这里调用android中的方法
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
相关文章推荐
- 将网页嵌入到android应用中
- Android中WebView在加载网页的时候,怎样应用本地的CSS效果?就是说怎样把本地的CSS嵌入到HTML中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- Android应用开发-小巫CSDN博客客户端之嵌入有米广告
- 22、Android Intent应用,打开网页,安装程序,打电话,打开地图等功能
- 应用中嵌入网页控件控制
- Android应用中嵌入Unity3D视图
- android菜鸟测试之Aj_02的Android 中自定义View的应用,在main.xml中嵌入自己写的view
- cocos2d-x 游戏嵌入到ios/android应用内部 - android篇
- Android应用嵌入广告相关经验收集整理
- 在网页中调启Android应用
- AndroidIntent应用,打开网页,安装程序,打电话,打开地图等功能 .
- android应用基于局域网部署网页---上传图片
- Android内嵌网页webview点击其中的链接跳转到我们应用内的Activity
- 程序进入Android Market(网页或者应用)
- 现在很多的手机应用,都可能会直接嵌入一个web页面。这样做的好处:一个是功能更新方便,维护起来容易,只需要维护服务器的页面即可,不需要更新客户端;另一个是功能通用,不仅Android可以用,iOS也可
- Android 通过网页链接打开本地应用
- [Android]webview直接加载网页<允许JS,进度条,当前应用内跳转>