• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Android硬件加速以及WebView的onPause,onResume,OnDestroy

JavaScript 水墨上仙 2462次浏览

Android硬件加速以及WebView的onPause,onResume,OnDestroy

MainActivity如下:



package cn.testwebview;  
import java.util.Timer;  
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;  
/** 
 * Demo描述: 
 * 1 硬件加速的实现 
 * 2 WebView的暂停/回复、销毁 
 *  
 * 参考资料: 
 * 1 http://stackoverflow.com/questions/5267639/how-to-safely-turn-webview-zooming-on-and-off-as-needed 
 * 2 http://qianxuechao.blog.163.com/blog/static/91813499201292541451358/ 
 *   Thank you very much 
 */  
public class MainActivity extends Activity {  
    private WebView mWebView;  
    private boolean isOnPause = false;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        hardwareAccelerate();  
        initWebView();  
    }  
      
    //硬件加速  
    private void hardwareAccelerate(){  
        if (this.getPhoneSDKInt() >= 14) {  
            getWindow().setFlags(0x1000000, 0x1000000);  
        }  
    }  
      
    //设置WebView  
    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.requestFocus();  
          
        //以下两句和硬件加速有关  
        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.loadUrl("http://www.cdmetro.cn/");  
        mWebView.setWebViewClient(new TestWebViewClient());  
    }  
      
      
    /** 
     * 当Activity执行onPause()时让WebView执行pause 
     */  
    @Override  
    protected void onPause() {  
        super.onPause();  
        try {  
            if (mWebView != null) {  
                mWebView.getClass().getMethod("onPause").invoke(mWebView, (Object[]) null);  
                isOnPause = true;  
            }  
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    /** 
     * 当Activity执行onResume()时让WebView执行resume 
     */  
    @Override  
    protected void onResume() {  
        super.onResume();  
        try {  
            if (isOnPause) {  
                if (mWebView != null) {  
                    mWebView.getClass().getMethod("onResume").invoke(mWebView, (Object[]) null);  
                }  
                isOnPause = false;  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    /** 
     * 该处的处理尤为重要: 
     * 应该在内置缩放控件消失以后,再执行mWebView.destroy() 
     * 否则报错WindowLeaked 
     */  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        if (mWebView != null) {  
            mWebView.getSettings().setBuiltInZoomControls(true);  
            mWebView.setVisibility(View.GONE);  
            long delayTime = ViewConfiguration.getZoomControlsTimeout();  
            new Timer().schedule(new TimerTask() {  
                @Override  
                public void run() {  
                    mWebView.destroy();  
                    mWebView = null;  
                }  
            }, delayTime);  
  
        }  
        isOnPause = false;  
    }  
      
      
    private class TestWebViewClient extends WebViewClient{  
        @Override  
        public void onPageStarted(WebView view, String url, Bitmap favicon) {  
            super.onPageStarted(view, url, favicon);  
        }  
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {  
            view.loadUrl(url);  
            return true;  
        }  
        @Override  
        public void onPageFinished(WebView view, String url) {  
            super.onPageFinished(view, url);  
              
        }  
        @Override  
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {  
            super.onReceivedError(view, errorCode, description, failingUrl);  
        }  
    }  
      
    public int getPhoneSDKInt() {  
        int version = 0;  
        try {  
            version = Integer.valueOf(android.os.Build.VERSION.SDK);  
        } catch (NumberFormatException e) {  
            e.printStackTrace();  
        }  
        return version;  
    }  
}  

main.xml如下:



<RelativeLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
  >  
  
    <WebView  
        android:id="@+id/webView"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:text="@string/hello_world"  
        android:layout_centerInParent="true"  
    />  
  
</RelativeLayout>  
 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Android硬件加速以及WebView的onPause,onResume,OnDestroy
喜欢 (0)
加载中……