Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android event for internet connectivity state change [duplicate]

I am making an app where a user is uploading information and files to my server on a somewhat frequent basis. This is done in new threads through a dedicated uploader service.

I know from this thread

Detect whether there is an Internet connection available on Android

that you can check if there is an internet connection relatively easily. You can also get socketTimeoutExceptions to detect internet connectivity issues. All that is well and good, and lets me cache my uploads easily enough when the connection didn't work for whatever reason.

My question though is how do I know when to reattempt the upload? Is there an event triggered when the connection is restored? Or am I stuck making a new thread that sleeps and then checks internet connectivity every 30 seconds or something?

Any ideas would be appreciated!

like image 911
akhalsa Avatar asked May 29 '11 16:05

akhalsa


People also ask

How does Android check for Internet connection?

In a wireless network the device may have a valid TCP/IP connection to the network but have no or limited connection to the Internet. Android NetworkMonitor service does check the Internet connection by trying to access special servers: http://connectivitycheck.gstatic.com/generate_204. http://www.google.com/gen_204.

How do I check programmatically for Internet connection?

Check Internet Connection Status In android, we can determine the internet connection status easily by using getActiveNetworkInfo() method of ConnectivityManager object. Following is the code snippet of using the ConnectivityManager class to know whether the internet connection is available or not.

How can you check if you are connected to the Internet before any network operations in Android?

Checking Network ConnectiongetSystemService(Context. CONNECTIVITY_SERVICE); Once you instantiate the object of ConnectivityManager class, you can use getAllNetworkInfo method to get the information of all the networks. This method returns an array of NetworkInfo.


1 Answers

very old post but i would like to share my receiver

no need to put your hands on manifest or other boring resources :)

USAGE

YOUR ACTIVITY:

/*  * You need to implement NetworkStateReceiverListener.  * This interface is described inside the NewtworkStateReceiver class  */ public class MyActivity implements NetworkStateReceiverListener {     /* ... */     private NetworkStateReceiver networkStateReceiver; } 

IN YOUR ACTIVITY: INSTANTIATE THE RECEIVER

public void onCreate(Bundle savedInstanceState) {     /* ... */     networkStateReceiver = new NetworkStateReceiver();     networkStateReceiver.addListener(this);     this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION)); }  public void onDestroy() {     super.onDestroy();     networkStateReceiver.removeListener(this);     this.unregisterReceiver(networkStateReceiver); } 

IN YOUR ACTIVITY: IMPLEMENTS THE REQUIRED METHODS

@Override public void networkAvailable() {     Log.d("tommydevall", "I'm in, baby!");     /* TODO: Your connection-oriented stuff here */ }  @Override public void networkUnavailable() {     Log.d("tommydevall", "I'm dancing with myself");     /* TODO: Your disconnection-oriented stuff here */         } 

THE RECEIVER

just copy and paste into your project as NetworkStateReceiver.java

public class NetworkStateReceiver extends BroadcastReceiver {      protected Set<NetworkStateReceiverListener> listeners;     protected Boolean connected;      public NetworkStateReceiver() {         listeners = new HashSet<NetworkStateReceiverListener>();         connected = null;     }      public void onReceive(Context context, Intent intent) {         if(intent == null || intent.getExtras() == null)             return;          ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);         NetworkInfo ni = manager.getActiveNetworkInfo();          if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {             connected = true;         } else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {             connected = false;         }          notifyStateToAll();     }      private void notifyStateToAll() {         for(NetworkStateReceiverListener listener : listeners)             notifyState(listener);     }      private void notifyState(NetworkStateReceiverListener listener) {         if(connected == null || listener == null)             return;          if(connected == true)             listener.networkAvailable();         else             listener.networkUnavailable();     }      public void addListener(NetworkStateReceiverListener l) {         listeners.add(l);         notifyState(l);     }      public void removeListener(NetworkStateReceiverListener l) {         listeners.remove(l);     }      public interface NetworkStateReceiverListener {         public void networkAvailable();         public void networkUnavailable();     } } 

ENJOY ;)

like image 170
Tommaso Resti Avatar answered Oct 29 '22 23:10

Tommaso Resti