Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are There a way to know if the internet(not network) disconnected

 
Mohammed Al rifai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear
i want to know how i can detect the internet disconnected (by unplug the network cable from the router ) i solved network problem (shutdown router ) and i did it for internet by catching the exception but i am searching if there's better way like android.net.conn.CONNECTIVITY_CHANGE
 
Mohan Prasath
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use ConnectivityManager class for that. The implementation would look like
ConnectivityManager con = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return con.getActiveNetworkInfo().isConnectedOrConnecting();


Also you have to give the following permission in the AndroidManifest <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I hope this is what you are looking for. If you want to look for connectivity changes while you are working in your application, you can always use a BroadcastReceiver to catch the network connectivity changes. I believe the following code would do that.

BroadcastReceiver receiver= new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
Log.w("Network Connectivity Listener", "Network connections Changed");
}
};

IntentFilter intent= new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver, intent);

 
Mohammed Al rifai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mohan
Thank you a lot for your time
I have used this already but this will detected network failure for example when router is shutdown but will not detect when the internet disconnected if the router working and you unplug the cable of the network
 
Mohan Prasath
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. I have tested my code in your scenario. It still shows it is connected to network when there is no Internet Connection. . I'm working on this one.

For a work around, hit any websites using Http request, if you have network connection but no internet you will receive "CONNECTION_TIMED_OUT" exception. This might help you for now I Hope.
 
Mohammed Al rifai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you , i have implemented it using exception but was wonder if there is way similar(Broadcast) and i will keep search on and if i found solution i will keep you updated
 
Mohammed Al rifai
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again
 
Mohan Prasath
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome . It's sad I can only give you work around. I will update you if I find any real solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic