I am trying to check network connectivity in my android application. For this, I am creating an object of ConnectivityManager to get the Connectivity_Service after that I am storing the ActiveNetwork Information in object of NetworkInfo class. Then, I am checking if the networkInfo object is null or not. I am getting a not null object so, I am setting a networkConnected flag = true and returning but both the statements if(networkConnected) as well as else get executed. And I get result false (which is in the else block). This is my code :-
public static boolean isNetworkConnected(Context ctx)
{
ConnectivityManager connMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
Boolean networkConnected = false;
if (netInfo != null)
{
if(netInfo.isConnectedOrConnecting())
{
networkConnected = true;
}
}
// The following if-else flow is surprising. Both of them are executed, even if
// the networkConnected flag is true or false.
// In both the cases false is returned.
// WHEN NETWORK IS CONNECTED, CONTROL ENTERS "if" BLOCK AND ALSO ENTERS "else" BLOCK, FINALLY RETURNING FALSE.
if(networkConnected)
{
return true;
}
else
{
return false;
}
}
I am using eclipse JUNO on windows 7. Also I did search on google and SO but I didn't find any relevant help in this context. (They have examples where in if-else condition is wrong or some malformed parenthesis, none of them are close to this particular querry).
Kindly help, Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire