Problem in understanding broadcast receiver?

Try something like this public class MyActivity extends Activity { private MyConnectivityListener connListener = null; private IntentFiler connIntentFilter = null; private Boolean connIntentFilterIsRegistered = false; @Override protected void onCreate(...) { ... connIntentFilter = new IntentFilter("android.net.conn. CONNECTIVITY_CHANGE"); connListener = new MyConnectivityListener(); } @Override protected void onResume() { ... if (!connIntentFilterIsRegistered) { registerReceiver(connListener, connIntentFilter); connIntentFilterIsRegistered = true; } } @Override protected void onPause() { ... if (connIntentFilterIsRegistered) { unregisterReceiver(connListener); connIntentFilterIsRegistered = false; } } protected class MyConnectivityListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // The NetworkInfo for the affected network is sent // as an extra; it should be consulted to see what // kind of connectivity event occurred. } } } A BroadcastReceiver is effectively a 'listener' which listens for events either sent by the system or, in some cases, by your own application components In this case, the system broadcasts android.net.conn.

CONNECTIVITY_CHANGE whenever there is a connection change (connected/disconnected). By registering your BroadcastReceiver to 'listen' for that event, you can get the extra included in the Intent from your BroadcastReceiver's onReceive(...) method and do whatever you need to do accordingly. The extra is a `NetworkInfo object which will contain information about the particular network and whether it is connected or not.

Try something like this... public class MyActivity extends Activity { private MyConnectivityListener connListener = null; private IntentFiler connIntentFilter = null; private Boolean connIntentFilterIsRegistered = false; @Override protected void onCreate(...) { ... connIntentFilter = new IntentFilter("android.net.conn. CONNECTIVITY_CHANGE"); connListener = new MyConnectivityListener(); } @Override protected void onResume() { ... if (!connIntentFilterIsRegistered) { registerReceiver(connListener, connIntentFilter); connIntentFilterIsRegistered = true; } } @Override protected void onPause() { ... if (connIntentFilterIsRegistered) { unregisterReceiver(connListener); connIntentFilterIsRegistered = false; } } protected class MyConnectivityListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // The NetworkInfo for the affected network is sent // as an extra; it should be consulted to see what // kind of connectivity event occurred. } } } A BroadcastReceiver is effectively a 'listener' which listens for events either sent by the system or, in some cases, by your own application components.In this case, the system broadcasts android.net.conn.

CONNECTIVITY_CHANGE whenever there is a connection change (connected/disconnected). By registering your BroadcastReceiver to 'listen' for that event, you can get the extra included in the Intent from your BroadcastReceiver's onReceive(...) method and do whatever you need to do accordingly. The extra is a `NetworkInfo object which will contain information about the particular network and whether it is connected or not.

If I put this connIntentFilter = new IntentFilter("android.net.conn. CONNECTIVITY_CHANGE"); in my onCreate() is this the thing that tells my broadcast when the state of the network changes? I don't need something else ...? Secondly,in onReceive() I verify what kind of connection is...and if is WiFi or something like it means that I could restart my thread...ya?.....And if is connected and is good could I start my thread in onReceive()?

Or how this part of restarting of the thread works? Thank you for clearing up the things a little bit more:) – george Jun 4 at 21:25 @george: The bit in onCreate() is simply there to initialize connIntentFilter with the name of the 'event' you want to listen out for. You then pass it to registerReceiver(...) along with the instance of your BroadcastReceiver.By registering, you tell the system that you want to be notified of a connection change.

By checking the NetworkInfo in onReceive you can tell if the 'event' is a connection or disconnection so start/stop your thread accordingly. – MisterSquonk Jun 4 at 21:59 Could you please take a look on the edit on my question,a few last questions:D! Thank you a lot – george Jun 4 at 22:23 @george: I think you're pretty much there.

To answer one question - no, the receiver won't continue to work once you leave your Activity if you implement the code in my example for unregistering the receiver in onPause() (which is recommended). If you need it to work at all times then things get more complicated. I suggest you experiment with it as it is now and get to know some more about how BroadcastReceivers and Intent broadcasts work.

– MisterSquonk Jun 4 at 22:46 If I start my app and I have internet connection it connects to the remote server but if I remove the internet connection It won't display any System.out.println() .....or not reconnect to my remote server – george Jun 4 at 22:47.

For that I'm hardly trying to implement a broadcast receiver which LISTENS for connectivity, but I'm having problems in understanding the concept. When there is connection to the Internet I'm sending data through the socket, when there is not I'm storing the data in a database. And when the Internet appears I'm restarting my thread to reconnect and send the old data (which hasn't been sent because of network crashing) and the new one.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions