Android硬件加速以及WebView的onPause,onResume,OnDestroy
feige 分享于 38分钟前
6854阅
0人收藏此代码,
我要收藏
Android硬件加速以及WebView的onPause,onResume,OnDestroy
MainActivity如下:
import java.util.TimerTask; |
import android.os.Bundle; |
import android.view.View; |
import android.view.ViewConfiguration; |
import android.webkit.WebSettings; |
import android.webkit.WebView; |
import android.webkit.WebViewClient; |
import android.webkit.WebSettings.PluginState; |
import android.app.Activity; |
import android.graphics.Bitmap; |
public class MainActivity extends Activity { |
private WebView mWebView; |
private boolean isOnPause = false ; |
protected void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.main); |
private void hardwareAccelerate(){ |
if ( this .getPhoneSDKInt() >= 14 ) { |
getWindow().setFlags( 0x1000000 , 0x1000000 ); |
private void initWebView() { |
mWebView = (WebView) findViewById(R.id.webView); |
mWebView.setVerticalScrollBarEnabled( false ); |
mWebView.setHorizontalScrollBarEnabled( false ); |
mWebView.getSettings().setSupportZoom( true ); |
mWebView.getSettings().setBuiltInZoomControls( true ); |
mWebView.getSettings().setJavaScriptEnabled( true ); |
mWebView.getSettings().setDomStorageEnabled( true ); |
mWebView.getSettings().setPluginsEnabled( true ); |
mWebView.getSettings().setPluginState(PluginState.ON); |
mWebView.getSettings().setAllowFileAccess( true ); |
mWebView.getSettings().setUseWideViewPort( true ); |
mWebView.getSettings().setLoadWithOverviewMode( true ); |
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); |
mWebView.setWebViewClient( new TestWebViewClient()); |
* 当Activity执行onPause()时让WebView执行pause |
protected void onPause() { |
mWebView.getClass().getMethod( "onPause" ).invoke(mWebView, (Object[]) null ); |
* 当Activity执行onResume()时让WebView执行resume |
protected void onResume() { |
mWebView.getClass().getMethod( "onResume" ).invoke(mWebView, (Object[]) null ); |
* 应该在内置缩放控件消失以后,再执行mWebView.destroy() |
protected void onDestroy() { |
mWebView.getSettings().setBuiltInZoomControls( true ); |
mWebView.setVisibility(View.GONE); |
long delayTime = ViewConfiguration.getZoomControlsTimeout(); |
new Timer().schedule( new TimerTask() { |
private class TestWebViewClient extends WebViewClient{ |
public void onPageStarted(WebView view, String url, Bitmap favicon) { |
super .onPageStarted(view, url, favicon); |
public boolean shouldOverrideUrlLoading(WebView view, String url) { |
public void onPageFinished(WebView view, String url) { |
super .onPageFinished(view, url); |
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { |
super .onReceivedError(view, errorCode, description, failingUrl); |
public int getPhoneSDKInt() { |
version = Integer.valueOf(android.os.Build.VERSION.SDK); |
} catch (NumberFormatException e) { |
main.xml如下:
android:layout_width= "match_parent" |
android:layout_height= "match_parent" |
android:id= "@+id/webView" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:text= "@string/hello_world" |
android:layout_centerInParent= "true" |
|