webview 的核心内容
//1、Intent意图打开网页:
String url="http://www.baidu.com/";
Uri uri=new Uri(url);
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent); //直接调用默认的浏览器
//1、加载本地资源:
webView.loadUrl("file:///android_asset/example.html");
//2、加载web资源:(调用系统默认浏览器)
webView.loadUrl("http://www.baidu.com");
//使页面获得焦点:
webView.requestFocus();
//使用WebView加载页面:
webView.loadUrl();
//使页面获得焦点:否则有的输入框不会有响应
webView.requestFocus();
//PS:需要在manifest中申请联网权限
思路:
1.使用WebView加载页面
–要在WebView加载页面,使用loadUrl()
–web资源:webView.loadUrl(“http://www.baidu.com“);
(本地文件使用: webView.loadUrl(“file:///android_asset/xxxx.html”);)
–使页面获得焦点: webView.requestFocus(); //否则有的输入框不会有响应
//2.获取网络访问权限:
//在它有效工作之前,你要保证你的应用能访问网络,要访问网络
//需要在配置文件activitymanifest.xml中获取INTERNET权限:
<uses-permission android:name="android.permission.INTERNET">
覆盖打开网页:
//覆盖webView默认通过默认浏览器或是第三方浏览器开网页,使网页可以在WebView中打开
//返回值时true的时候控制网页在WebView中去打开,如果为false调用系统浏览器或第三方浏览器
//WebViewClient帮助WebView去处理一些页面控制和请求通知
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
});
webview中网页的前进与后退
//重写系统的onKeyDoun()方法
//并写入webView.goBack()和goForward()来向前向后访问已访问的站点
//改写物理按键的逻辑
public boolean onKeyDown(int keyCode,keyEvent event ){
if(keyCode==keyEvent.KEYCODE_BACK){ //如果按下的按钮等于“返回键”
if(webview.canGoBack()){
webview.goBack();//返回上一个页面
return true;
} else {
System.exit(0);//退出程序
}
}
}
启动支持 JavaScript:
WebSettings websettings=webView.getSettings();
websettings.setJavaScriptEnabled(true);
Ps: finish()与System.exit(0)区别
finish是Activity的方法,仅针对Activity,当调用finish(),只将活动推向后台,没有立即释放内存,Activity的资源并没有被清理
System.exit(0)时,杀死了整个进程,这时候活动所占的资源也会被释放。其实android的机制决定了用户无法完全退出应用,当你的application最长时间没有被用过的时候,android自身会决定将application关闭了。
源代码↓↓↓↓↓
package com.imooc.android_webview;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private String url = "http://2014.qq.com/";
private WebView webView;
private ProgressDialog dialog;// 进度条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
// Uri uri = Uri.parse(url); //url为你要链接的地址
// Intent intent =new Intent(Intent.ACTION_VIEW, uri);
// startActivity(intent);
init();// 具体方法在下面
}
private void init() {
// TODO Auto-generated method stub
webView = (WebView) findViewById(R.id.webView);
// WebView加载本地资源
// webView.loadUrl("file:///android_asset/example.html");
// WebView加载web资源
webView.loadUrl(url);
/* 覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,
使得网页可以在WebVIew中打开 */
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
/* 返回值是true的时候控制网页在WebView中去打开,
如果为false调用系统浏览器或第三方浏览器去打开 */
view.loadUrl(url);
return true;
}
// WebViewClient帮助WebView去处理一些页面控制和请求通知
});
// 启用支持JavaScript
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
// WebView加载页面优先使用缓存加载
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
// newProgress 1-100之间的整数
if (newProgress == 100) {
// 网页加载完毕,关闭ProgressDialog
closeDialog();
} else {
// 网页正在加载,打开ProgressDialog
openDialog(newProgress);
}
}
private void closeDialog() {
// TODO Auto-generated method stub
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
private void openDialog(int newProgress) {
// TODO Auto-generated method stub
if (dialog == null) {
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
} else {
dialog.setProgress(newProgress);
}
}
});
}
// 改写物理按键——返回的逻辑
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Toast.makeText(this, webView.getUrl(),
// Toast.LENGTH_SHORT).show();
if (webView.canGoBack()) {
webView.goBack();// 返回上一页面
return true;
} else {
System.exit(0);// 退出程序
}
}
return super.onKeyDown(keyCode, event);
}
}
布局文件↓↓↓↓↓↓
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>