2013-12-12 61 views
0

我使用的WebView顯示HTML data.It工作正常,如果數據量少(小於1 MB)加載網頁視圖頁中的數據..我用下面的代碼如何通過網頁中的Android

mDecryptDataWv.setWebViewClient(new WebViewClient() { 


     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      DebugLog.i(TAG, "Processing webview url click..."); 
      view.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      //showProgressDialog(); 
      DebugLog.i(TAG, "************onPageStarted **************"); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      DebugLog.i(TAG, "************onPageFinished **************"); 

      if (mProgressDialog != null) { 
       if (mProgressDialog.isShowing()) { 
        DebugLog.i(TAG, "mProgressDialog ::::::stopping"); 
        mProgressDialog.dismiss(); 
        mProgressDialog = null; 

       } 
      } 

如果數據超過1MB,則需要更多時間才能完成加載。因此,我的意圖是在用戶滾動進度對話框時逐頁加載數據。任何人都有想法?

回答

0

//我用這個類和我的代碼在我身邊的工作FIME請嘗試這可能是它會幫助你

public class WebViewActivity extends Activity { 
    private WebView webview; 
    private static final String TAG = "Main"; 
    private ProgressDialog progressBar; 
    private TextView header_maintext; 
    private TextView headeroptiontext; 
    private RelativeLayout back; 
    private String url_string="http://www.google.com"; 
    private String header_maintext_string="Your text"; 

    /** Called when the activity is first created. */ 
    @SuppressLint("SetJavaScriptEnabled") @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.webview_layout); 


     webview = (WebView)findViewById(R.id.webview01); 
     header_maintext= (TextView)findViewById(R.id.header_maintext); 
     header_maintext.setText(header_maintext_string); 

     headeroptiontext = (TextView)findViewById(R.id.headeroptiontext); 
     headeroptiontext.setVisibility(View.GONE); 

     WebSettings settings = webview.getSettings(); 
     settings.setJavaScriptEnabled(true); 
     webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
     webview.getSettings().setLoadWithOverviewMode(true); 
     webview.getSettings().setUseWideViewPort(true); 

      back = (RelativeLayout) findViewById(R.id.back_layout); 
      back.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) 
      { 
       // TODO Auto-generated method stub 
       if(webview.canGoBack() == true) 
        { 
         webview.goBack(); 
        } 
       else 
        { 
         finish(); 
        } 
      } 
     }); 

     final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 

     progressBar = ProgressDialog.show(WebViewActivity.this, "My application", "Loading..."); 

     webview.setWebViewClient(new WebViewClient() { 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 

       view.loadUrl(url); 
       return true; 
      } 

      public void onPageFinished(WebView view, String url) { 
       Log.i(TAG, "Finished loading URL: " +url); 
       if (progressBar.isShowing()) { 
        progressBar.dismiss(); 
       } 
      } 

      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 

       Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
       alertDialog.setTitle("Error"); 
       alertDialog.setMessage(description); 
       alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         return; 
        } 
       }); 
       alertDialog.show(); 
      } 
     }); 
     webview.loadUrl(url_string); 


    } 
    @Override 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if(event.getAction() == KeyEvent.ACTION_DOWN){ 
      switch(keyCode) 
      { 
      case KeyEvent.KEYCODE_BACK: 
       if(webview.canGoBack() == true){ 
        webview.goBack(); 
       }else{ 
        finish(); 
       } 
       return true; 
      } 

     } 
     return super.onKeyDown(keyCode, event); 
    } 
}