我正在开发一个 android 项目,如果没有互联网连接,当我按下刷新按钮时,它必须显示 toast 消息。如果页面加载时没有互联网连接,我实现了这个。但是如何实现刷新按钮中的相同代码?
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;
}
}
}
这是我的webview类
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();
}
}
});
}
}
}
最佳答案
@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();
}
关于Android 刷新按钮检查互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26863954/