-3

What i need Precisely is my code should check internet status at regular intervals (Say every 30 or 40 seconds ).How to do this ..Should i use Demon thread or any other components available in android .Hope am clear .

fazil
  • 1,155
  • 11
  • 24

3 Answers3

1

try this one to check internet connection availability

public boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity =   (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
    for (int i = 0; i < info.length; i++)
        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }
    return false;
}

Use this one to check every 40 sec

Timer  networkTimer = new Timer();
NetWorkTimerTask networkTimerTask = new NetWorkTimerTask();
networkTimer.schedule(networkTimerTask, 0,40*1000);
public class NetWorkTimerTask extends TimerTask {

    @Override
    public void run() {
        networkHandler.sendEmptyMessage(0);
    }
}; 
@SuppressLint("HandlerLeak")
Handler networkHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        isOnline = isNetworkAvailable(getApplicationContext());
    }
};

Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Ramki Anba
  • 754
  • 1
  • 10
  • 21
0
public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

and add this

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
  • 1
    This code shows me the status of internet connection like it is true or false.I had already implemented this. what i need is check this internet connection regularly for every 10 or 15 seconds .It should run in back ground checking the net connection regularly ..How to check in back ground regularly.. – fazil Jun 02 '14 at 07:29
0
public boolean isOnline() {
        ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMgr.getActiveNetworkInfo();

        if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
            /*
             * Toast.makeText(getActivity(), "No Internet connection!",
             * Toast.LENGTH_LONG).show();
             */
            return false;
        }
        return true;
    }

and Call it like

if (isOnline()) {
//code if net available
}
else
{
Toast.makeText(MainActivity.this,"No Internet connection available  ",
                            Toast.LENGTH_SHORT).show();
}

And if you want to check on Interval then put if condition in thread

Android
  • 8,995
  • 9
  • 67
  • 108