this is my connection class
public class Datacon extends Activity {
public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
}
这是mywebview类
public class Mywebpage<Bitmap> extends Activity {
ProgressBar loadingProgressBar,loadingTitle;
Boolean isInternetPresent = false;
ConnectionDetector cd;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Context context = this;
setContentView(R.layout.webviewpage);
AdView ads=(AdView)this.findViewById(R.id.adViewer);
AdRequest request=new AdRequest.Builder().build();
ads.loadAd(request);
View mActionBarView = getLayoutInflater().inflate(R.layout.my_actionbar, null);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradientwhite));
Button search = (Button) actionBar.getCustomView().findViewById(R.id.button1);
search.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent inte=new Intent(Mywebpage.this,MainActivity.class);
inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inte);
finish();
}
}) ;
Bundle extras = getIntent().getExtras();
String title;
final String url;
if (!Datacon.checkInternetConnection(this)) {
for (int i=0; i < 3; i++)
{
Toast.makeText(this, "Check your Internet Connection!", Toast.LENGTH_LONG).show();
}
} else {
if (extras != null) {
title = extras.getString("title");
url = extras.getString("url");
TextView text=(TextView) findViewById(R.id.textView1);
text.setText(title);
final WebView myWebView =(WebView)findViewById(R.id.WebView);
myWebView.loadUrl(url);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
loadingProgressBar=(ProgressBar)findViewById(R.id.progressBar1);
myWebView.setWebViewClient(new WebViewClient(){
public void onPageStarted(WebView view, String url, Bitmap favicon) {
loadingProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
loadingProgressBar.setVisibility(View.GONE);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
Button refresh = (Button) actionBar.getCustomView().findViewById(R.id.but2);
cd = new ConnectionDetector(getApplicationContext());
refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConnectivityManager con_manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = con_manager.getActiveNetworkInfo();
String status = netInfo.getState().toString();
if (status.equals("CONNECTED")) {
myWebView.loadUrl(url);
}
else {
Context context_new = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context_new, text, duration);
toast.show();
}
}
});
}
}
} }
答案 0 :(得分:1)
@Override
public void onClick(View v) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
Toast.makeText(getActivity(), "no internet", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getActivity(), "internet available", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
您的onClick没有引用互联网检查方法。试试这个:
@Override
public void onClick(View v) {
if (Datacon.checkInternetConnection(getActivity())) {
myWebView.loadUrl(url);
}else{
Toast.makeText(getActivity(), "no internet", Toast.LENGTH_SHORT).show();
}
}
答案 2 :(得分:0)
您需要点击checkInternetConnection
按钮致电Refresh
:
Context context= this;
refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkInternetConnection(context)) {
Toast.makeText(context, "No Internet connection", Toast.LENGTH_LONG).show();
}
else{
myWebView.loadUrl(url);
}
}
});
答案 3 :(得分:0)
您的refresh
按钮已存在于保存Internet连接方法的活动中。所以只需在onClick
中调用该方法,如:
refreshButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkInternetConnection(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "No Internet connection", Toast.LENGTH_LONG).show();
}
else{
myWebView.loadUrl(url);
}
}
});
希望它有所帮助。