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.
Asked
Active
Viewed 152 times
2 Answers
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
-
But it will check the connection only when i will call this method. what i want is it can notify itself if network is gone. – Kamal Sep 09 '11 at 08:54
-
try to use broadcast receiver
-
could you please post this as a new Answer in place of Comment.? – Kamal Sep 09 '11 at 08:58
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
-
I am sorry, but I am not able to get this link due to Firewall or something like that. – Kamal Sep 09 '11 at 08:56