WebView未加载网页 [英] WebView is not loading webpage

查看:68
本文介绍了WebView未加载网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WebView加载网站.但是它非常慢,并且在加载特定网站时会泄漏. 我正在使用以下代码加载WebView.

I am using WebView for loading a website. But it is very slow and is leaking when specific websites are loaded. I am loading WebView with the following code.

@Override
    protected void onNewIntent(Intent intent) {
        if (intent.getStringExtra("url") != null) {
            webView.loadurl(intent.getStringExtra("url"));

            }
    }

但是我用以下方法初始化WebView后,在onCreate()方法中调用webView.loadUrl(Config.URL);(Config.URL可能包含与上面指定的相同的URL).

But I am calling webView.loadUrl(Config.URL); (Config.URL may contain same url as specified above) in onCreate() method after initializing WebView with the following.

        this.webView = (WebView) findViewById(R.id.wv);
        this.webView.getSettings().setJavaScriptEnabled(true);
        this.webView.getSettings().setLoadsImagesAutomatically(true);
        this.webView.getSettings().setDomStorageEnabled(true);
        this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        MyClient client = new MyClient(WebActivity.this, (ProgressBar)findViewById(R.id.progressBar));
        webView.setWebViewClient(client);

onCreate()加载a正常(不好,太慢了).但 从onNewIntent()加载的相同URL 不起作用!. 在onNewIntent()中执行此操作后,没有使用URL加载 webView.loadurl(),当前页面变得不动. IE.这 滚动条在WebView中移动,但页面未滚动.我测试了 onCreate()中的相同URL,并且可以正常工作.

Loading a from onCreate() is working fine (not fine, it's too slow). But the same URL that is loading from onNewIntent() is not working!!!. After I did this inonNewIntent() no URLs got loaded using webView.loadurl() and the current page is getting immovable. ie. the scrollbars are moving in WebView but page is not scrolling. I tested the same URL in onCreate() and it is working.

为此,我通过

intent.putExtra("url", Config.URL+targetUrl);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

,带有通知中的待定意图.尽管它可以在某些设备(例如Google Nexus)中运行.但这在大多数手机上不起作用. 我有

with the pending intent from the notifications. Although it is working in some devices i.e Google Nexus. But it is not working on most of the phones. I have

android:hardwareAccelerated="true"

我的客户

public class MyClient extends WebViewClient{
    private Context context;
    private Activity activity;
    private Handler handler;
    private Runnable runnable;
    private ProgressBar viewBar;
    private String ret,ret2;
    public void setFirstLoad(boolean firstLoad) {
        this.firstLoad = firstLoad;
    }

    private boolean firstLoad=false;
    public MyClient(Activity activity, ProgressBar bar) {
        this.context = activity.getApplicationContext();
        this.activity = activity;
        viewBar=bar;
        handler=new Handler();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        /*if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_DIAL,
                    Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            *//*view.setVisibility(View.GONE);
            viewBar.setVisibility(View.VISIBLE);*//*
            view.loadUrl(url);
        }
        return true;*/
        if (Uri.parse(url).getHost().equals("www.somepage.com")) {
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            Answers.getInstance().logShare(new ShareEvent()
            .putContentId(Build.USER)
            .putMethod(shareName(url))
            .putContentName(contentDecode(url))
            .putContentType("news_share"));
        }catch (android.content.ActivityNotFoundException e){
            Log.e("Activity not found",e.toString());
            Toast.makeText(context,"Application not found",Toast.LENGTH_LONG).show();
        }

        return true;

    }

    @Override
    public void onReceivedError(final WebView view, int errorCode, String description, final String failingUrl) {
        //Clearing the WebView
        try {
            view.stopLoading();
        } catch (Exception e) {
        }
        try {
            view.clearView();
        } catch (Exception e) {
        }
        if (view.canGoBack()) {
            view.goBack();
        }
        view.loadUrl("about:blank");

        //Showing and creating an alet dialog
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
        alertDialog.setTitle("Error");
        alertDialog.setMessage("No internet connection was found!");
        alertDialog.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                view.loadUrl(failingUrl);

            }
        });
        AlertDialog alert = alertDialog.create();
        alert.show();

        //Don't forget to call supper!
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public void onLoadResource(final WebView view, String url) {
        super.onLoadResource(view, url);
        //injectScriptFile(view, "js/script.js");
        injectCSS(view,"css/style.css");
        if (firstLoad){
            firstLoad=false;
            view.setVisibility(View.INVISIBLE);
            viewBar.setVisibility(View.VISIBLE);
            runnable=new Runnable() {
                @Override
                public void run() {
                    viewBar.setVisibility(View.GONE);
                    view.setVisibility(View.VISIBLE);
                }
            };
            handler.postDelayed(runnable,2000);
        }

        // test if the script was loaded
       // view.loadUrl("javascript:setTimeout(hideMe(), 200)");
    }

    /*@Override
    public void onPageFinished(final WebView view, String url) {

        //System.gc();
    }*/


    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        System.gc();
    }

问题是:onNewIntent()中使用loadurl()方法时出了什么问题?

The question is: What is the problem when using loadurl() method in onNewIntent()?

推荐答案

从加载Webview的位置尝试此操作

Try this from where you loads webview

   web.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

这篇关于WebView未加载网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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