开发者

Android app crash when first network is switched off and then switched on

I am using the following function to c开发者_开发百科heck network connectivity but application crashes when wifi status is swapped

    public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connec = (ConnectivityManager) context
    .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connec.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnected() == true) {
        return true;
    }
    return false;
}


it took some time to shift between networks...

So Now if we disable the wifi it automatically get connected to mobile network after few seconds.. and if we enable the wifi then it get connected to wifi network again...

The thread in your application is checking the connectivity before that shift...

check the conversation here

Android: How to Enable/Disable Wifi or Internet Connection Programmatically


Are you missing a NullPointerException?

I use the following method:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    } catch(NullPointerException n) {
        return false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜