4

Hello world how to always detect if I am connected or disconnected from the Internet automatically. Example: When I connected and I turn off 3G or wifi the toast says I'm disconnected, 3 seconds after I activate the 3G or wifi and a toast tells me I am connected.

I use this method but it tells me the state of the connection at startup of the application but not during navigation in the application.

 ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

//For 3G check
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
//For WiFi Check
    boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();


    if (!is3g && !isWifi)
    {
        Toast.makeText(getApplicationContext(),"Network Connection is OFF", Toast.LENGTH_LONG).show();
    }
    else
    {
        Toast.makeText(getApplicationContext(),"Network Connection is ON", Toast.LENGTH_LONG).show();

    }

Help me please

Prasad
  • 3,462
  • 1
  • 23
  • 28
slama007
  • 1,273
  • 2
  • 18
  • 34

5 Answers5

3

You can use a utility class and BroadcastReceiver for this purpose. Below link shows show step by step procedure

http://viralpatel.net/blogs/android-internet-connection-status-network-change/

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
3

You could use Timer's schedule to run a thread. Notice the last 3000 which will make it run every 3 seconds.

Timer timer = new Timer();
timer.schedule(new TimerTask()
{
    @Override
    public void run()
    {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);

        //For 3G check
        boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
        //For WiFi Check
        boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();


        if (!is3g && !isWifi)
        {
            Toast.makeText(getApplicationContext(),"Network Connection is OFF", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"Network Connection is ON", Toast.LENGTH_LONG).show();

        }
    }
}, 0, 3000);
Smittey
  • 2,475
  • 10
  • 28
  • 35
3

You can simply use this method anywhere you like :)

 public static boolean isDeviceOnline(Context context) {

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    boolean isOnline = (networkInfo != null && networkInfo.isConnected());
    if(!isOnline)
        Toast.makeText(context, " No internet Connection ", Toast.LENGTH_SHORT).show();

    return isOnline;
}

Inside Your activity use this code:

activity=this;
if(isDeviceOnline(activity))
 {
 //you are online
 }
 else
 {
 //you are offline
 } 
Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
1

The answer is BroadcastReceiver! You get notified everytime your network connectivity change from WIFI/3g/ON/OFF..

Look here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

And here: http://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/

denis_lor
  • 6,212
  • 4
  • 31
  • 55
-2
private boolean checknet(){

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkinfo = connectivityManager.getActiveNetworkInfo();

        return activeNetworkinfo != null && activeNetworkinfo.isConnected();
    }

//write below methodcall in every button click event wherever u want to check internet connection


 if(!checknet())
        {

            Toast.makeText(getApplicationContext(), "No Internet Connecction.", Toast.LENGTH_SHORT).show();

        }
MBT
  • 21,733
  • 19
  • 84
  • 102
  • While this code might answer the question you still might consider adding a few explanatory sentences as this increases the value of your answer for other users. – MBT Aug 24 '18 at 08:12