Webview中设置字体,可以使用 WebView.getSettings().setDefaultFontSize() 方法,设置屏幕的缩放级别可以使用 WebView.getSettings().setDefaultZoom() 来实现。
目前在Android 2.2中已经加入了Adobe Flash Player功能,我们可以WebView.getSettings().setPluginsEnabled(true); 设置允许Gears插件来实现网页中的Flash动画显示。
Webview可以帮助我们设计内嵌专业的浏览器,相对于部分以省流量需要服务器中转的那种 html解析器来说有本质的区别,因为它们没有JavaScript脚本解析器,Android123认为未来这种方式不会有什么发展空间,代表软件Opera Mini以及国内的一些名为“XX浏览器”。
在WebView中显示AlertDialog窗口提示,这里我们通过JavaScript脚本直接互通,在一个WebView中直接显示js的alert可以重写onJsAlert方法即可。
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
{
new AlertDialog.Builder(myApp)
.setTitle("Android开发网")
.setMessage(message) //从形参中传递的,同时还有String url等等
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm(); //从形参的结果中分析
}
})
.setCancelable(false)
.create()
.show();
return true;
};
webview退出,播放器关闭
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.FrameLayout;
public class WebviewActivity extends Activity
{
WebView playView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
playView = (WebView) findViewById(R.id.webviewshow);
WebSettings webSettings = playView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setPluginState(PluginState.ON);
playView.loadUrl("file:///android_asset/swf_view.html");
}
private void callHiddenWebViewMethod(String name)
{
if (playView != null)
{
try
{
Method method = WebView.class.getMethod(name);
method.invoke(playView);
}
catch (NoSuchMethodException e)
{
Log.i("No such method: " + name, e.toString());
}
catch (IllegalAccessException e)
{
Log.i("Illegal Access: " + name, e.toString());
}
catch (InvocationTargetException e)
{
Log.d("Invocation Target Exception: " + name, e.toString());
}
}
}
@Override
protected void onPause()
{
super.onPause();
playView.pauseTimers();
if (isFinishing())
{
playView.loadUrl("about:blank");
setContentView(new FrameLayout(this));
}
callHiddenWebViewMethod("onPause");
}
@Override
protected void onResume()
{
super.onResume();
callHiddenWebViewMethod("onResume");
}
}
清空webview缓存
1.删除保存于手机上的缓存.
调用:clearCacheFolder(Activity.getCacheDir(), System.currentTimeMillis());//删除此时之前的缓存.
2. 打开关闭使用缓存:
优先使用缓存:
1.WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
不使用缓存:
1.WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
3.在退出应用的时候加上如下代码: