2013-12-14 47 views
1

即使页面加载完成,Progress Bar也不会解雇 我试图在后台加载webview中的url,以至于用户无法识别它是一些其他网站打开..我希望他们认为,无论是在web视图中打开是应用程序本身的一部分。因此,我试图在后台打开url,直到时间url加载我想进度条出现,但作为页面加载我正在解雇进度条,但它继续出现..它不是解雇,这就是问题..请帮我出Android:即使页面加载完成,Progress Bar也不会解雇

这里是我的代码...

package com.example.urlopeningwithprogressbar; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

ProgressDialog pg; 
Background bg; 
WebView wv; 
Button b1; 

static boolean buttonpressed=false; 

@SuppressLint("SetJavaScriptEnabled") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    b1=(Button)findViewById(R.id.button1); 
    wv = (WebView)findViewById(R.id.webView1); 

    b1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      bg = new Background(); 
      bg.execute(); 


     } 
    }); 


} 
public void progress(){ 
    pg = new ProgressDialog(MainActivity.this); 
    pg.setTitle(""); 
    pg.setMessage("Please Wait........."); 
    pg.setCancelable(false); 
    pg.setIndeterminate(true); 
    pg.show(); 
} 

class Background extends AsyncTask<Void, Void, Void> 
{ 

    @SuppressLint("SetJavaScriptEnabled") 
    @Override 
    protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 


      try{ 
       wv.loadUrl("URL"); 
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 

       wv.getSettings().setJavaScriptEnabled(true); 
       wv.getSettings().setLoadWithOverviewMode(true); 
       wv.getSettings().setUseWideViewPort(true); 
       wv.getSettings().setBuiltInZoomControls(true); 
      // wv.setWebViewClient(new ourViewClient()); 



     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { 

     Toast.makeText(MainActivity.this, "Process Complete", Toast.LENGTH_SHORT).show(); 


    } 

    @Override 
    protected void onPreExecute() { 
     Toast.makeText(getApplicationContext(), "pre execute is working??", Toast.LENGTH_SHORT).show(); 
     progress(); 
    } 
} 
public class myWebClient extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 

      view.loadUrl(url); 
      return true; 

     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 

      pg.dismiss(); 
     } 
    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

+0

onPageFinished叫? – FWeigl

+0

请参阅:http://stackoverflow.com/questions/3903895/how-to-show-progress-bar-on-webview – Aneeshmg

+0

@Ascorbin onPageFinished不叫...... –

回答

2

你永远不会实例化或使用myWebClient类。 你必须

wv.setWebViewClient(new myWebClient()); 

实际使用它。 另请注意,类名应该是大写。

+0

非常感谢好友:)它的工作现在:)我希望我可以投票支持你,但是因为我刚刚加入了前一天的stackoverflow,所以没有超过15的声望,所以不能投票给你 –