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!
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.
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.
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.
very old post but i would like to share my receiver
no need to put your hands on manifest or other boring resources :)
/* * You need to implement NetworkStateReceiverListener. * This interface is described inside the NewtworkStateReceiver class */ public class MyActivity implements NetworkStateReceiverListener { /* ... */ private NetworkStateReceiver networkStateReceiver; }
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); }
@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 */ }
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With