0

I am working on an application which uses internet on every Activity. I am checking the connection onCreate of every Activity. But if n/w(Internet) is gone in between how can i know. is there any Method which can notify whenever the Network has gone during Application.

Kamal
  • 1,415
  • 13
  • 24

2 Answers2

1

in each Activity class you can add this broadcast receiver in the onReceive method you can interact with the activity this is an example

public class MyActivity extends Activity {
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(...) {
            ...
        }
   });

    public void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");

        this.registerReceiver(this.receiver, filter);
    }

    public void onPause() {
        super.onPause();

        this.unregisterReceiver(this.receiver);
    }
}

this way the receiver is instantiated when the class is created (could also do in onCreate). Then in the onResume/onPause I handle registering and unregistering the receiver. Then in the reciever's onReceive method I do whatever is necessary to make the activity react the way I want to when it receives the broadcast.

confucius
  • 13,127
  • 10
  • 47
  • 66
0

There is a CONNECTIVITY_CHANGE broadcast which you can listen to get the updates about the connection. Here are some more details http://groups.google.com/group/android-developers/browse_thread/thread/ce1c3ed3e39a0c81

denis.solonenko
  • 11,645
  • 2
  • 28
  • 23