简体   繁体   English

每次单击链接时,webview中的进度对话框

[英]Progress Dialog in webview every time a link is clicked

This question has been asked before but, I'm a newbe and having issues trying to get it to work. 之前曾有人问过这个问题,但我是个新手,在尝试使其工作时遇到了问题。 My question is how do I get a Progress Dialog to show up everytime a user clicks a link in webview. 我的问题是,每当用户单击Webview中的链接时,如何显示进度对话框。 I have a dialog that shows when the app is first started but, does not show a dialog when the links are clicked. 我有一个对话框,该对话框显示首次启动该应用程序的时间,但是单击链接时不显示对话框。 I'm pretty sure that the solution is here , but I just cant seem to put them together properly for it to work..... Can someone please help me put this together. 我很确定解决方案在这里 ,但是我似乎无法正确地将它们组合在一起以使其正常工作.....有人可以帮助我将其组合在一起。

My Code 我的密码

See the approved anwser 查看批准的答案

I didn't get any help with my problem but I finally figured it out. 我的问题没有得到任何帮助,但我终于明白了。 I'm posting an example project to help anyone with the same problem. 我正在发布一个示例项目,以帮助任何遇到相同问题的人。 This is a web_view project that shows a (page Loading...) dialog, every time a link is clicked. 每次单击链接时,这是一个web_view项目,显示一个(页面加载中...)对话框。 This example also checks for an Internet connection... 此示例还检查Internet连接...

Download the Eclipse project here 在此处下载Eclipse项目

Here are my two java files. 这是我的两个Java文件。 The file below contains the solution for the progress dialog. 以下文件包含进度对话框的解决方案。

     package com.example.project;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebActivity extends Activity {

    private WebView wv;

    private String LASTURL = "";

    Menu myMenu = null;
    private ProgressDialog dialog;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

        if (!InternetConnection.checkNetworkConnection(this)) {
            showAlert(this, "No Data Connection", "This Application requires an internet connection");
        } else {

            setContentView(R.layout.web_view);

            wv = (WebView) findViewById(R.id.web_view);

            WebSettings webSettings = wv.getSettings();
            webSettings.setSavePassword(true);
            webSettings.setSaveFormData(true);
            webSettings.setJavaScriptEnabled(true);
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setSupportZoom(false);


            final Activity activity = this;

            // start ProgressDialog with "Page loading..."
            dialog = new ProgressDialog(activity);
            dialog.setMessage("Page loading...");
            dialog.setIndeterminate(true);
            dialog.setCancelable(true);
            dialog.show();

            wv.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    // set address bar and progress
                    // activity.setTitle( " " + LASTURL );
                    // activity.setProgress( progress * 100 );

                    if (progress == 100) {
                        // stop ProgressDialog after loading
                        dialog.dismiss();

                        // activity.setTitle( " " + LASTURL );
                    }
                }
            });

            wv.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    Toast.makeText(getApplicationContext(),
                            "Error: " + description + " " + failingUrl,
                            Toast.LENGTH_LONG).show();
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.indexOf("google") <= 0) {
                        // the link is not for a page on my site, so launch
                        // another Activity that handles URLs
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                                .parse(url));
                        startActivity(intent);
                        return true;
                    }
                    return false;
                }
                /*****************************************************************/
                /*  Here the load of the page will start so we must launch the  */
                /*  ProgressDialog                                              */
                /*****************************************************************/                                             
                public void onPageStarted(WebView view, String url,
                        Bitmap favicon) {

                    // this is what we should do
                    dialog.setMessage("Page loading...");
                    dialog.setIndeterminate(true);
                    dialog.setCancelable(true);
                    dialog.show();
                    //
                    LASTURL = url;
                    view.getSettings().setLoadsImagesAutomatically(true);
                    view.getSettings().setBuiltInZoomControls(true);
                }
                /*****************************************************************/
                /*  Here the load of the page will stop so we must dismiss the  */
                /*  ProgressDialog                                              */
                /*****************************************************************/ 
                public void onPageFinished(WebView view, String url) {
                    // this is what we should do
                    dialog.dismiss();

                }
            });
            wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            wv.setScrollbarFadingEnabled(false);
            wv.loadUrl("http://www.google.com");

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
            wv.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    /*****************************************************************/
    /*  Here is a menu with basic options. Will probably get 
     * comments on how this is replaced by action bar               */
    /*****************************************************************/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        this.myMenu = menu;
        MenuItem item = menu.add(0, 1, 0, "Home");
        item.setIcon(R.drawable.home);
        MenuItem item2 = menu.add(0, 2, 0, "Back");
        item2.setIcon(R.drawable.arrowleft);
        MenuItem item3 = menu.add(0, 3, 0, "Reload");
        item3.setIcon(R.drawable.s);
        MenuItem item4 = menu.add(0, 4, 0, "Share");
        item4.setIcon(R.drawable.share);
        MenuItem item5 = menu.add(0, 5, 0, "Rate");
        item5.setIcon(R.drawable.vote);
        MenuItem item6 = menu.add(0, 6, 0, "Exit");
        item6.setIcon(R.drawable.close);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
            wv.loadUrl("http://www.google.com");
            break;
        case 2:
            if (wv.canGoBack()) {
                wv.goBack();
            }
            break;
        case 3:
            wv.loadUrl(LASTURL);
            break;
        case 4:
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("plain/text");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Check out this app I found.");
            startActivity(Intent.createChooser(sharingIntent,"Share using"));
            break;
        case 5:

            Intent marketIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(
                    "http://market.android.com/details?id=" + getPackageName()));
                  startActivity(marketIntent2);
                break;

        case 6:
            finish();
            break;

        }

        return true;
    }



    /**
     * Display a simple alert dialog with the given text and title.
     * 
     * @param context
     *            Android context in which the dialog should be displayed
     * @param title
     *            Alert dialog title
     * @param text
     *            Alert dialog message
     */
    public void showAlert(Context context, String title, String text) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        // set title
        alertDialogBuilder.setTitle( title);

        // set dialog message
        alertDialogBuilder
        .setMessage( text )
        .setCancelable(false)
        .setPositiveButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // if this button is clicked, close
                // current activity
                finish();
            }
        })
        .create().show();

    }
}

This java file checks for a Internet Connection. 此java文件检查Internet连接。 If no connection is available the application will close. 如果没有可用的连接,则应用程序将关闭。

package com.example.project;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class InternetConnection {

    public static boolean checkNetworkConnection(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        boolean infoResult = false;
        boolean wifiResult = false;
        boolean mobileResult = false;

        try {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (info == null) {
                return false;
            } else {
                infoResult = info.isConnectedOrConnecting();
                }

            NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifi == null) {
                return false;
            } else {
                wifiResult = wifi.isConnectedOrConnecting();
                }

            NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mobile == null) {
                return false;
            } else {
                mobileResult = mobile.isConnectedOrConnecting();
                }

            // if(wifi.isConnectedOrConnecting()||mobile.isConnectedOrConnecting())

        } catch (NullPointerException nullPointException) {
            System.out.println( nullPointException.getMessage() );
        }

        return infoResult||wifiResult||mobileResult;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM