Android Internet Connection Crash

113 Views Asked by At

My App does a ConnectionCheck before connecting a html-website and parsing some information with that code:

public boolean NetworkInfo(Context context) {
// Netzwerk Infos anzeigen

if (context != null) {
    ConnectivityManager mgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (mgr != null) {
        boolean mobileNetwork = false;
        boolean wifiNetwork = false;

        NetworkInfo mobileInfo = mgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiInfo = mgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mobileInfo != null)
            mobileNetwork = mobileInfo.isConnected();
        if (wifiInfo != null)
            wifiNetwork = wifiInfo.isConnected();

        return (mobileNetwork || wifiNetwork);
    }
}
return false;
}

But during the connection the internet-state could change and the app doesn't receive all requested information.

How can I protect the App from crashing because of missing data? Is there something which checks the connection the whole time during the thread is running?

0

There are 0 best solutions below