今天对接 要一定程度上控制h5集成的代码
这里就要进行一定的拦截了
贴出示例代码
package com.i.free;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ZWebViewActivity extends Activity
{
WebView webView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView=(WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
// webView.getSettings().setPluginsEnabled(true);// 可以使用插件
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDefaultTextEncodingName("UTF-8");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // 设置 缓存模式
// 开启 DOM storage API 功能 //支持文件存储
webView.getSettings().setDomStorageEnabled(true);
// 开启 database storage API 功能 //支持数据库存储
webView.getSettings().setDatabaseEnabled(true);
String cacheDirPath = getFilesDir().getAbsolutePath()
+ APP_CACAHE_DIRNAME;
// String cacheDirPath =
// getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME;
// Log.i(TAG, "cacheDirPath=" + cacheDirPath);
// 设置数据库缓存路径
webView.getSettings().setDatabasePath(cacheDirPath);
// 设置 Application Caches 缓存目录
webView.getSettings().setAppCachePath(cacheDirPath);
// 开启 Application Caches 功能
webView.getSettings().setAppCacheEnabled(true);
webView.setVisibility(View.VISIBLE);
// WebView加载web资源
webView.loadUrl("http://139.196.208.240:801/wap/edu/news.aspx");
webView.getSettings().setDomStorageEnabled(true);
}
WebViewClient wClient=new WebViewClient()
{
/*
* shouldOverrideUrlLoading:在web页面里单击链接的时候,会自动调用android自带的浏览器来打开链接,需要通过该方法在本页面打开;
*/
public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url)
{
System.out.println("url = "+url);
view.loadUrl("http://www.360buy.com/");//不能是.html后缀的,不清楚为什吗。
//显示自己的对话框
//showDialog();
// 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
return true;
};
/*
* 加载资源时响应
*/
public void onLoadResource(android.webkit.WebView view, String url)
{
//System.out.println("加载资源时响应");
};
/*
* 在加载页面时响应
*/
public void onPageStarted(android.webkit.WebView view, String url, android.graphics.Bitmap favicon)
{
//System.out.println("在加载页面时响应");
};
/*
* 在加载页面结束时响应
*/
public void onPageFinished(android.webkit.WebView view, String url)
{
//System.out.println("在加载页面结束时响应");
};
/*
* 在加载出错时响应
*/
public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl)
{
//System.out.println("在加载出错时响应");
};
/*
* request数据时调用
*/
public void onReceivedHttpAuthRequest(android.webkit.WebView view, android.webkit.HttpAuthHandler handler, String host, String realm)
{
};
};
public void showDialog()
{
AlertDialog.Builder aBuilder=new AlertDialog.Builder(this);
aBuilder.setNegativeButton
(
"确定",
new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
}
);
aBuilder.create().show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
{
webView.goBack();
{
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}