2012-05-27 60 views
0

這個問題之前已經被問到過,但是我是一個新手,並且有問題試圖讓它工作。我的問題是,如果每次用戶點擊webview中的鏈接,我將如何獲得進度對話框。我有一個對話框,顯示應用程序首次啓動時的狀態,但點擊鏈接時不顯示對話框。我敢肯定,解決方案是here,但我似乎不能把它們放在一起,以使其正常工作.....有人可以幫助我把它放在一起。每次點擊鏈接時在webview中的進度對話框

我的代碼

見批准anwser

回答

1

我沒有得到我的任何問題的幫助,但我終於想通了。我發佈了一個示例項目來幫助任何有同樣問題的人。這是一個web_view項目,每次單擊鏈接時都會顯示(頁面加載...)對話框。這個例子也檢查互聯網連接...

Download the Eclipse project here

這裏是我的兩個Java文件。以下文件包含進度對話框的解決方案。

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

    } 
} 

此java文件檢查Internet連接。如果沒有連接可用,應用程序將關閉。

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; 
    } 
} 
+0

您能否在您的答案中詳細說明您的解決方案,而不是僅僅依賴可能在某一點上斷開的鏈接? – Flexo