Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Programmatically check internet connection and display dialog if notConnected

I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the app crashes. Basically, my problem is to check programmatically that is mobile is connected to internet or not. if not then don't fetch the data from webservice into webview and display a dialog box showing "Check your internet connection"

while doing research i found many things, and i have tried to implement that. but, its not satisfying my requirement

my code is,

public boolean isOnline() {     ConnectivityManager cm =         (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);     NetworkInfo netInfo = cm.getActiveNetworkInfo();     if (netInfo != null && netInfo.isConnectedOrConnecting()) {         return true;     }     else     {         Description.setVisibility(View.INVISIBLE);         new AlertDialog.Builder(WelcomePage.this)         .setTitle(getResources().getString(R.string.app_name))         .setMessage(                 getResources().getString(                         R.string.internet_error))         .setPositiveButton("OK", null).show();     }     return false; } 

i am calling this function in doInBackground() of AsyncTask

Please Help!

like image 289
Java_Android Avatar asked Jul 26 '13 11:07

Java_Android


People also ask

How do I check programmatically for internet connection?

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.

How do I check network connection status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.


1 Answers

You could checkout this library:

https://github.com/novoda/merlin

You just implement Connectable and you will get a callback when the network goes down or comes up.

Therefore you can show your dialog in this scenario.

You can also query the library for the current state and choose not to do your network task

example

Create Merlin (using Merlin.Builder())

merlin = new Merlin.Builder().withConnectableCallbacks().build(context); 

Bind and unbind the service in your activity

@Override protected void onResume() {     super.onResume();     merlin.bind(); }  @Override protected void onPause() {     super.onPause();     merlin.unbind(); } 

Register for callbacks

merlin.registerConnectable(new Connectable() {         @Override         public void onConnect() {             // Do something!         } }); 

The MerlinActivity within the demo shows a simple way to declutter Merlin from your main application code.

like image 137
Blundell Avatar answered Sep 23 '22 23:09

Blundell