Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法检查可用的Internet_Java_Android_Android Studio_Url_Android Webview - Fatal编程技术网

Java 无法检查可用的Internet

Java 无法检查可用的Internet,java,android,android-studio,url,android-webview,Java,Android,Android Studio,Url,Android Webview,我正在开发一个具有web视图的android应用程序。当我想在显示默认消息之前检查Internet是否可用时,就会出现问题。 我已经研究了这些链接和。我不知道该怎么做。 这是我的密码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Web= findView

我正在开发一个具有web视图的android应用程序。当我想在显示默认消息之前检查Internet是否可用时,就会出现问题。 我已经研究了这些链接和。我不知道该怎么做。 这是我的密码

    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");
}

使用此方法检查internet连接

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;
    }
}
之后,您可以像这样创建对象并检查internet连接

碎片

ConnectionDetector connectionDetector = new ConnectionDetector();
connectionDetector.check_internet(getContext())
活动

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
    }

什么意思?你试过并面对过任何错误吗?然后把那个错误贴出来。。。会发生什么?它显示页面了吗?有什么问题吗?有什么问题吗?在setContentView(R.layout.activity_main)之后我已经写了上面的代码,该代码放在哪里;在上面我已经写了setContentView调用后,将代码放在哪里只要在if(connectionDetector.check_internet(this))中签入它