我想添加一项功能,当用户点击将您带到网站的按钮时,它应该通知您当前未连接或没有可用的网络连接。目前我有这个代码,但它似乎只是带我直接到网站(离线无法连接)但显示没有信息?
这是我的代码:
next7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
}
public void checkNetworkStatus(){
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =
connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if( wifi.isAvailable() ){
Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
}
else if( mobile.isAvailable() ){
Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();
}
}
任何帮助将不胜感激。感谢。
答案 0 :(得分:2)
像这样做
public boolean checkNetworkStatus() {
boolean isConected = false;
final ConnectivityManager connMgr = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable()) {
isConected = true;
Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show();
} else if (mobile.isAvailable()) {
isConected = true;
Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
}
return isConected;
}
和
if(checkNetworkStatus())
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
else{
//No internet
}