Webview在Ice Cream Sandwitch中显示白色空白页 [英] Webview shows white blank page in Ice Cream Sandwitch

查看:131
本文介绍了Webview在Ice Cream Sandwitch中显示白色空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页浏览中,我加载了一个URL,其中包含一个电视频道直播的嵌入式视频播放器。除了ICS(4)之外,它在Android的所有操作系统版本中都能正常工作。第一次它很好地播放视频,但是当我回到包含视频的那个页面再来时,视频不会加载并显示空白页面。如果我强制停止应用程序设置并再次启动应用程序然后它运行良好然后像往常一样再次出现白屏,我已经实施了很多策略,这是最新的,我完全被困在这里:

In my webview I have loaded a URL which have an embeded video player of a tv channel live stream. It is working correctly in all the OS version of Android except ICS(4). First time It plays the video well, but when I go back and come again in that page containing the video then the video doesnt loads and shows a blank white page. If I force stop the app from the application setting and start the app again then It runs well then appears white screen again as usual, I have implemented a lot of tactics and this is the latest , I am totally stuck here:

public class Livetvwebview extends Activity {

    RelativeLayout a;
    WebView webtv;
    String url;
    VideoView video;
    WChromeClient chromeClient;
    WebViewClient wvClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.livewebview);
        Toast.makeText(getApplicationContext(),
                "Channel is loading..This may take upto a minute",
                Toast.LENGTH_LONG).show();
        url = getIntent().getStringExtra("tvchannel");
        Log.i("TVURL", url);
        webtv = (WebView) findViewById(R.id.webViewlive);
        webtv.clearCache(true);
        webtv.loadUrl(url);

        webtv.getSettings().setLoadWithOverviewMode(true);
        webtv.getSettings().setUseWideViewPort(true);

        webtv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webtv.setWebChromeClient(new WChromeClient());
        webtv.setWebViewClient(new myWebClient());
        webtv.getSettings().setJavaScriptEnabled(true);

        webtv.getSettings().setPluginState(PluginState.ON);

        webtv.getSettings().setDomStorageEnabled(true);
    }



    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub

            super.onPageStarted(view, url, favicon);

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        WebSettings webSettings = webtv.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webtv.onPause();
        this.finish();
        super.onPause();

    }

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {



        webtv.onResume();
        super.onResume();

    }






    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        // android.os.Process.killProcess(android.os.Process.myPid());
        Editor editor = getSharedPreferences("clear_cache",
                Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
        trimCache(this);

        super.onDestroy();
    }

    class WChromeClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            Log.i("Method", "Onrogresschanged");

            Livetvwebview.this.setTitle("Loading...");
            Livetvwebview.this.setProgress(progress * 100);
            if (progress == 100)
                Livetvwebview.this.setTitle("LiveTv");

        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // TODO Auto-generated method stub
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    webtv.setVisibility(View.GONE);
                    video = (VideoView) frame.getFocusedChild();
                    FrameLayout.LayoutParams par = new FrameLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT);
                    par.gravity = Gravity.CENTER_HORIZONTAL;
                    video.setLayoutParams(par);
                    frame.removeView(video);
                    a.addView(video);
                    video.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            Toast.makeText(Livetvwebview.this,
                                    "Video completed", Toast.LENGTH_LONG)
                                    .show();

                        }
                    });

                    video.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what,
                                int extra) {
                            Toast.makeText(Livetvwebview.this,
                                    "Encountered some error", Toast.LENGTH_LONG)
                                    .show();
                            return true;
                        }
                    });
                    video.start();
                }

            }

        }
    }

    public static void trimCache(Context context) {
        try {
            File dir = context.getCacheDir();
            if (dir != null && dir.isDirectory()) {
                deleteDir(dir);

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }


        return dir.delete();
    }
}

有人可以帮我吗?

推荐答案

经过长时间的研究,我自己发现当我写入onPause()

After researching for a long long time I myself figured out that when I write in the onPause()

webtv.destroy();

而不是

webtv.onPause();

解决问题:)

这篇关于Webview在Ice Cream Sandwitch中显示白色空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆