您的位置:首页 > 移动开发

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