再会!请帮忙!我如何加入ProgressDialog
+ BroadcastReceiver
.?我使用库 jsoup 在单独的线程中加载页面。作为格鲁吉亚:显示ProgressDialog
。但是,如果您不想使用 Toast broadtsastretseiver 上网,那没有互联网。
这是代码活动:
package com.demo;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled")
public class ObedActivity extends Activity {
WebView mWebView;
ProgressDialog mProgress;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.registerReceiver(this.mConnReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
mWebView = (WebView) findViewById(R.id.www);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new DemoClient());
mWebView.setBackgroundColor(0);
mWebView.setBackgroundResource(R.drawable.bg);
mProgress = ProgressDialog.show(this, "Загружаю.", "Нужно подождать...");
Thread downloadThread = new Thread(
new Runnable() {
public void run() {
Document doc = null;
try {
doc = Jsoup.connect("http://google.ru").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements ele = doc.select("div#sample");
String html = ele.toString();
mWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
}
}
);
downloadThread.start();
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if(currentNetworkInfo.isConnected()){
}else{
Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
}
}
};
private class ObedActivityClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}