android使用webview访问网页
2017-01-23 16:16
411 查看
代码
public class WebViewActivity extends FragmentActivity { private final static String TAG = WebViewActivity.class.getSimpleName(); private WebView webView; private Set<String> unUseUrlSet; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_web_view); JYBoxApplication jyBoxApplication = JYBoxApplication.getInstance(); final String serviceAddress = jyBoxApplication.getServiceAddress(); unUseUrlSet = new HashSet<String>(); unUseUrlSet.add("http://"+serviceAddress+"/mobile/business.htm?select=4"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/member/myIndex.htm?select=5"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/index_member.htm?select=1"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/index_member.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/member/checkIn.htm?select=2"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/setting/index.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/outsign/preSign.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/work_register.htm"); webView = (WebView)findViewById(R.id.web_view); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBlockNetworkImage(false); webView.getSettings().setBlockNetworkLoads(false); webView.getSettings().setDomStorageEnabled(true); //webView.getSettings().<span style="font-family: STHeiti;">setJavaScriptEnabled(true);</span>; //webView.loadData("", "text/html", "UTF-8"); String url = getIntent().getStringExtra("url"); webView.loadUrl(url); webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } @Override public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { return super.onJsConfirm(view, url, message, result); } }); // sms:10086?body=%E5%BC%A0%E6%88%90%E9%BE%99%E9%82%80%E8%AF%B7%E6%82%A8%E5%8A%A0%E5%85%A5%E4%B8%8A%E6%B5%B7%E6%96%87%E6%80%9D%E6%B5%B7%E8%BE%89%E9%9B%86%E5%9B%A2,%E8%AF%B7%E7%82%B9%E5%87%BB%E9%93%BE%E6%8E%A5[http://www.jingin.cn/invite.htm?code=Pd9g4l]%E5%AE%8C%E6%88%90%E6%B3%A8%E5%86%8C%EF%BC%8C%E8%AF%A5%E9%93%BE%E6%8E%A524%E5%B0%8F%E6%97%B6%E5%86%85%E6%9C%89%E6%95%88%E3%80%82[%E7%BB%8F%E8%90%A5%E7%AE%A1%E5%AE%B6] webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { try { url = URLDecoder.decode(url,"UTF-8"); } catch (UnsupportedEncodingException e) { Log.e(TAG, "an error occured when collect crash info", e); } if (url.indexOf("tel:") > -1) { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); return true; }else if(url.indexOf("sms:") > -1){ Map map = null; try { map = URLUtil.splitQuery(url); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String body = ""; if(map!=null){ body = (String)(map.get("body")==null?"":map.get("body")); } Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", body); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent); }else if(unUseUrlSet.contains(url)){ if(url.equals("http://"+serviceAddress+"/mobile/index_member.htm")){ Intent intent = new Intent(WebViewActivity.this,IndexActivity.class); startActivity(intent); }else{ WebViewActivity.this.finish(); } }else{ view.loadUrl(url);// 使用当前WebView处理跳转 return true;//true表示此事件在此处被处理,不需要再广播 } return true; } @Override //转向错误时的处理 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(WebViewActivity.this, "加载异常:" + description, Toast.LENGTH_SHORT).show(); } }); ActivityManager.getInstance().addActivity(this); } ~~~ 注意事项 1.我们请求的网页如果包含地图 需要加上配置项 webView.getSettings().setDomStorageEnabled(true); 否则地图将无法显示 2.如果需要让网页上的js生效需要加入配置 webView.getSettings().setJavaScriptEnabled(true); 3.html5页面上如果有打电话发短信的功能,需要截取sms tel 脚本进行android本地化处理 // sms:10086?body=%E5%BC%A0%E6%88%90%E9%BE%99%E9%82%80%E8%AF%B7%E6%82%A8%E5%8A%A0%E5%85%A5%E4%B8%8A%E6%B5%B7%E6%96%87%E6%80%9D%E6%B5%B7%E8%BE%89%E9%9B%86%E5%9B%A2,%E8%AF%B7%E7%82%B9%E5%87%BB%E9%93%BE%E6%8E%A5[http://www.jingin.cn/invite.htm?code=Pd9g4l]%E5%AE%8C%E6%88%90%E6%B3%A8%E5%86%8C%EF%BC%8C%E8%AF%A5%E9%93%BE%E6%8E%A524%E5%B0%8F%E6%97%B6%E5%86%85%E6%9C%89%E6%95%88%E3%80%82[%E7%BB%8F%E8%90%A5%E7%AE%A1%E5%AE%B6] webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { try { url = URLDecoder.decode(url,"UTF-8"); } catch (UnsupportedEncodingException e) { Log.e(TAG, "an error occured when collect crash info", e); } //处理页面上的打电话的js if (url.indexOf("tel:") > -1) { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); return true; //处理页面上的发送短信js }else if(url.indexOf("sms:") > -1){ Map map = null; try { map = URLUtil.splitQuery(url); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String body = ""; if(map!=null){ body = (String)(map.get("body")==null?"":map.get("body")); } Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra("sms_body", body); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent); }else if(unUseUrlSet.contains(url)){ if(url.equals("http://"+serviceAddress+"/mobile/index_member.htm")){ Intent intent = new Intent(WebViewActivity.this,IndexActivity.class); startActivity(intent); }else{ WebViewActivity.this.finish(); } }else{ view.loadUrl(url);// 使用当前WebView处理跳转 return true;//true表示此事件在此处被处理,不需要再广播 } return true; } @Override //转向错误时的处理 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(WebViewActivity.this, "加载异常:" + description, Toast.LENGTH_SHORT).show(); } }); 4.处理页面的返回按钮需要退回到android的activity里面 可以将返回按钮要跳转的目标链接统一放到一个集合中,然后统一拦截后然后进行android本地化处理 unUseUrlSet = new HashSet<String>(); unUseUrlSet.add("http://"+serviceAddress+"/mobile/business.htm?select=4"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/member/myIndex.htm?select=5"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/index_member.htm?select=1"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/index_member.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/member/checkIn.htm?select=2"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/setting/index.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/outsign/preSign.htm"); unUseUrlSet.add("http://"+serviceAddress+"/mobile/work_register.htm"); }else if(unUseUrlSet.contains(url)){ if(url.equals("http://"+serviceAddress+"/mobile/index_member.htm")){ Intent intent = new Intent(WebViewActivity.this,IndexActivity.class); startActivity(intent); }else{ WebViewActivity.this.finish(); }
相关文章推荐
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android开发_WebView组件使用详解_LoadUrl直接显示网页内容
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android WebView的使用(禁止超链接调用其他浏览器 设置滚动条 禁止横竖屏切换重新加载网页 )
- Android如何使用WebView访问https的网站
- Android webview goback 使用cache中的网页
- Android 使用webview 打不开网页的解决办法
- android开发(30) 使用WebView,点击网页中的链接建立QQ 临时会话 WPA
- android 学习心得---webview使用得到网页加载时间
- Android网络编程实践之旅(四):使用WebView浏览网页
- Android 使用WebView访问https的url——处理SslError
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- android使用webview加载网页
- android WebView 网页控件 访问因特网 url
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- android中使用webview缓存网页
- Android 中访问网页 webView
- Android使用WebView浏览网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页