无法检查 Internet 可用
Unable to check the Internet Available
我正在开发一个具有网络视图的 android 应用程序。当我想在显示默认消息之前检查 Internet 是否可用时,问题就来了。
我研究了这些链接 Link1and link2。我很困惑如何做到这一点。
这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Web= findViewById(R.id.webview);
Web.getSettings().setJavaScriptEnabled(true);
Web.loadUrl("http://yourconsenthomebuilders.com/app/clients");
}
使用此方法检查互联网连接
public class ConnectionDetector {
public ConnectionDetector() {
}
public Boolean check_internet(Context context) {
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
}
return false;
}
}
之后您可以像这样创建对象并检查互联网连接
片段
ConnectionDetector connectionDetector = new ConnectionDetector();
connectionDetector.check_internet(getContext())
Activity
ConnectionDetector connectionDetector = new ConnectionDetector();
connectionDetector.check_internet(this)
setContentView() 之后
添加此逻辑
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isAvailable() && info.isConnected())
{
//network connected
//show web view logic here
}else{
//network not connected
//show alerts to users
}
我正在开发一个具有网络视图的 android 应用程序。当我想在显示默认消息之前检查 Internet 是否可用时,问题就来了。 我研究了这些链接 Link1and link2。我很困惑如何做到这一点。 这是我的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Web= findViewById(R.id.webview);
Web.getSettings().setJavaScriptEnabled(true);
Web.loadUrl("http://yourconsenthomebuilders.com/app/clients");
}
使用此方法检查互联网连接
public class ConnectionDetector {
public ConnectionDetector() {
}
public Boolean check_internet(Context context) {
if (context != null) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
}
return false;
}
}
之后您可以像这样创建对象并检查互联网连接
片段
ConnectionDetector connectionDetector = new ConnectionDetector();
connectionDetector.check_internet(getContext())
Activity
ConnectionDetector connectionDetector = new ConnectionDetector();
connectionDetector.check_internet(this)
setContentView() 之后 添加此逻辑
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null && info.isAvailable() && info.isConnected())
{
//network connected
//show web view logic here
}else{
//network not connected
//show alerts to users
}