Reputation: 1800
hi I am using this code in the login screen of my android app for checking the network connectivity is available or not. and is working fine.
public static boolean checkNetworkConnection(Context context) {
final ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnectedOrConnecting() ||mobile.isConnectedOrConnecting())
return true;
else
return false;
}
But I thought of a case --
My app is fetching some data from the server and showing in table.Now I opened the app and while fetching the data connection goes of.Now I dont want to refresh the app or page to fetch data from the server. I want a background process should run or thread which will keep checking for connectivity and as soon as it get the connection it should fetch data automatically.
Can any body help me with this.
Upvotes: 1
Views: 1479
Reputation: 2310
Use this code..
public boolean CheckInternet()
{
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true, otherwise false;
if (wifi.isConnected()) {
return true;
} else if (mobile.isConnected()) {
return true;
}
return false;
}
Upvotes: -1
Reputation: 553
You should never check periodically (or continuously) for connectivity. You should listen for connection changes, as stated here. The whole page is a good tutorial on how not to drain the baterry when using internet.
Basically, you need to register a BroadcastReceiver for <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
, and implement the logic you want there:
public class NetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager conn = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
// Checks the user prefs and the network connection. Based on the result, decides whether
// to refresh the display or keep the current display.
// If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection.
if (WIFI.equals(sPref) && networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
// If device has its Wi-Fi connection, sets refreshDisplay
// to true. This causes the display to be refreshed when the user
// returns to the app.
refreshDisplay = true;
Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show();
// If the setting is ANY network and there is a network connection
// (which by process of elimination would be mobile), sets refreshDisplay to true.
} else if (ANY.equals(sPref) && networkInfo != null) {
refreshDisplay = true;
// Otherwise, the app can't download content--either because there is no network
// connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there
// is no Wi-Fi connection.
// Sets refreshDisplay to false.
} else {
refreshDisplay = false;
Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show();
}
}
Upvotes: 3