android – WebView没有加载网页

栏目: IOS · Android · 发布时间: 6年前

内容简介:我正在使用WebView加载网站.但是,当加载特定网站时,这是非常慢的漏洞.我正在使用以下代码加载WebView.但我正在调用webView.loadUrl(Config.URL); (Config.URL可能包含与上述相同的url)在onCreate()方法中初始化以后的WebView.

我正在使用WebView加载网站.但是,当加载特定网站时,这是非常慢的漏洞.

我正在使用以下代码加载WebView.

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

            }
    }

但我正在调用webView.loadUrl(Config.URL); (Config.URL可能包含与上述相同的url)在onCreate()方法中初始化以后的WebView.

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);

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 in onNewIntent() 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.

为了做到这一点,我正在传递url

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

与通知的待决意图.虽然它在某些设备(即Google Nexus)中正在运行.但是大多数手机都不行.

我有

android:hardwareAccelerated="true"

Myclient

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()方法时有什么问题?


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据库系统概念

数据库系统概念

Abraham Silberschatz、Henry F. Korth、S. Sudarshan / 杨冬青、马秀莉、唐世渭 / 机械工业 / 2006-10-01 / 69.50元

本书是数据库系统方面的经典教材之一。国际上许多著名大学包括斯坦福大学、耶鲁大学、得克萨斯大学、康奈尔大学、伊利诺伊大学、印度理工学院等都采用本书作为教科书。我国也有许多所大学采用本书以前版本的中文版作为本科生和研究生的数据库课程的教材和主要教学参考书,收到了良好的效果。 本书调整和新增内容:调整了第4版的讲授顺序。首先介绍SQL及其高级特性,使学生容易接受数据库设计的概念。新增数据库设计的专......一起来看看 《数据库系统概念》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试