I have to connect my app with server using either wifi (if it is available),
or gprs (if wifi is not available).
Here is my code to check the connection availability
public static final boolean isConnectionAvailable(Activity a)
{
ConnectivityManager cm = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
State mobile = cm.getNetworkInfo(0).getState();
State wifi = cm.getNetworkInfo(1).getState();
if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING)
{
return true;
}
if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING)
{
return true;
}
return false;
}
Is this a correct way? Can anyone suggest me a better way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…