Android学习-使用WebView在app上显示网页
来源:互联网 发布:淘宝订单截图生成器 编辑:程序博客网 时间:2024/05/17 09:20
通过Intent调用系统浏览器
Uri uri = Uri.parse(url);//url为你要链接的地址Intent intent = new Intent(Intent.ACTION_VIEW,uri);startActivity(intent);
package com.example.angel.listviewpro;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainAcitivity extends AppCompatActivity { private String url ="http://www.baidu.com"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); Uri uri = Uri.parse(url);//url为你要链接的地址 Intent intent = new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); }}
使用WebView加载页面
要在WebView加载页面,使用loadUrl();
web资源:webView.loadUrl(“http://www.baidu.com“);
本地文件用:webView.load(“file//android_asset/xx.html”);
本地文件存放在:asset文件中
//使页面获得焦点(比方说页面中有文本框,可以输入东西)
webView.requestFocus();
获取网络访权限
在它有效工作之前,你要保证你的应用能访问网络,要访问网络,需要在你的配置文件中获取INTERNET权限。
<manifest....><uses-permission android:name="android.permission.INTERNET"/>....</manifest>
处理页面导航
当用户点击一个WebView中的页面的链接时,通常,是由默认的浏览器打开并加载目标URL的。然而,你可以在WebView中覆盖这一行为,那么链接就会在WebView中直接打开。
webView.setWebViewClient(new WebClient());
在WebView中使用JaveScript
如果你想要加载在WebView中的WEB界面使用Javascript,你需要在WebView中启用Javascript,启用JavaScript你可以通过WebView中带有的WebSettings来启用它。你可以通过getSettings来获取WebSetting的值,然后通过setJavaScriptEnabled()来启用JavaScript
WebView WebView = (WebView)findViewById(R.id.webView);WebSettings webSettings = webView.getSettings();webSettings.setJavaScriptEnabled(true);
后退与前进
当你的WebView覆盖了URL加载,他会自动生成历史访问记录,你可以通过goBack()或者goForward()向前或向后访问已访问过的站点
public boolean onKeyDown(int keyCode,KeyEvent event){ if((keyCode == KeyEvent.KEYCODE_BACK)&&webView.canGoBack(){ webView.goBack(); return true; } return super.onKeyDown(keyCode,event);}
判断页面加载过程
由于有些网页可能加载缓慢,所以我们需要去判断页面的加载过程,制作进度条给予用户良好的体验效果
webView.setWebChromeClient(new WebChromeClient(){ public void onProgressChanged(WebView view,int newProgress){ if(newProgress==100){ //加载完成 }else{ //加载中 } }});
WebView缓存的运用
优先使用缓存:
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
不使用缓存
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
package com.example.angel.listviewpro;import android.app.ProgressDialog;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.KeyEvent;import android.webkit.WebChromeClient;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;public class MainAcitivity extends AppCompatActivity { private WebView webView; private ProgressDialog dialog; private String url="http://www.163ren.com/index.php"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); init(); } private void init() { webView = (WebView) findViewById(R.id.webView); //webView加载web资源 webView.loadUrl(url); //覆盖WebView默认通过第三方或者是系统打开网页的行为,使网页可以在WebView中打开 webView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { //返回值是true的时候控制网页在WebView中打开, //为false的时候,调用系统浏览器或者第三方浏览器打开 view.loadUrl(url); return false; } //webViewCilent是帮助WebView去处理一些页面控制和请求通知 //public void onPageStarted(WebView view, String url, Bitmap favicon) //是处理页面开启时的操作 }); //启用支持JavaScript WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebChromeClient(new WebChromeClient(){ public void onProgressChanged(WebView view, int newProgress) { //newProgress 1-100之间的证书 if(newProgress == 100){ //加载完毕,关闭ProgressDialog closeDialog(); }else{ //正在加载,打开ProgressDialog openDialog(newProgress); } } private void closeDialog() { if(dialog != null && dialog.isShowing()){ dialog.dismiss(); dialog = null; } } private void openDialog(int newProgress) { if(dialog == null){ dialog = new ProgressDialog(MainAcitivity.this); dialog.setTitle("正在加载"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); } else{ dialog.setProgress(newProgress); } } }); } //改写物理按键返回的逻辑 public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
- Android学习-使用WebView在app上显示网页
- Android使用Webview显示网页
- android:使用webview显示网页
- Android-使用WebView显示网页
- Android-使用WebView显示网页
- Android-使用WebView视图显示网页
- Android——使用WebView显示网页
- Android中使用WebView显示网页
- android Android:控件WebView显示网页webview的使用
- Android--当webview加载网页的时候在标题栏上显示加载进度
- Android--当webview加载网页的时候在标题栏上显示加载进度
- Android--当webview加载网页的时候在标题栏上显示加载进度
- Android--当webview加载网页的时候在标题栏上显示加载进度
- Android:WebView显示网页
- 使用webView显示网页
- 使用WebView显示网页
- 使用WebView显示网页
- 使用webview显示网页
- sift算法知识总结
- ServletAndJsp学习笔记--4
- 解决extjs前端异步导出excel无提示问题
- C#编程入门10_面向对象之继承
- MQTT进阶篇
- Android学习-使用WebView在app上显示网页
- AI如何变革阿里电商?iDST首席科学家任小枫首次公开演讲
- 两日公开课:伯克利深度强化学习训练营 | 视频+PPT
- 阿里巴巴成立达摩院,马云投入1000亿,但未来得靠自己挣钱
- 英伟达为全自动驾驶汽车打造AI计算机,计算力达上代10倍
- 框架笔记
- 软银领投Petuum 9300万美元融资,CMU邢波想让AI走进普通企业
- 12年后,索尼重启机器人业务
- 英特尔生产17个量子位超导芯片,现已交付合作伙伴