4

I'm working on Internet based app,so i need to monitor Internet Connection. link

I used this code in on create of mainActivity to check my internet connection it works fine.

public boolean isOnline() 
{  
ConnectivityManager cm = (ConnectivityManager) 
getSystemService(Context.CONNECTIVITY_SERVICE);  return 
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

But i need this monitoring should be done through out application. Where should i use this ? Any AsyncTask needed?

Community
  • 1
  • 1
shanmugamgsn
  • 820
  • 5
  • 16
  • 27

2 Answers2

6

create a BroadcastReceiver to detect change in connection status. see Eric's answer here

Community
  • 1
  • 1
josephus
  • 8,284
  • 1
  • 37
  • 57
  • Thanks Josephus. You mean to say BroadcastReceiver will keep on checking internet connection at back end right? – shanmugamgsn Nov 11 '11 at 03:14
  • no it doesn't. android throws a broadcast when internet connection drops or changes status. you just need to catch that broadcast, so you don't need to check everytime. – josephus Nov 11 '11 at 03:34
  • oh its great... Eric's answer was good. but how do i intiaiate this Broadcast Reciever i mean onRevieve method? if i have toggle button to switch on/off Broadcast reciever.? – shanmugamgsn Nov 11 '11 at 03:43
  • As in Eric's code, Broadcast reciever will be running through out application right? – shanmugamgsn Nov 11 '11 at 03:44
  • BroadcastReceiver technically will not be "running throughout" the application, instead it just registers your application to receive the broadcast. It's not an "always alive" service if that's what you're worring about. – josephus Nov 11 '11 at 05:27
  • ya really i was worried about battery and performance of my app. thanks Josephus – shanmugamgsn Nov 11 '11 at 09:48
  • What in case the connection does not disconnect but as is typical a case in some not so developer countries the GPRS gets stuck. What will the broadcast return? Or should we use a timeout? – Skynet Feb 05 '14 at 16:16
  • @NunChai Yes, the only way to handle that would be to use some timeout mechanism. The implementation depends on what library you're using, which can be answered in a separate Question thread. – josephus Feb 05 '14 at 23:55
  • Thanks your comment has been valuable for me! – Skynet Feb 06 '14 at 05:38
4

Use a BroadCast Receiver, like so :

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
            String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                        boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

            NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

            // do application-specific task(s) based on the current network state, such 
            // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
        }
    };

 private void registerReceivers() {    
       registerReceiver(mConnReceiver, 
           new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
   }
Reno
  • 33,594
  • 11
  • 89
  • 102