5

Good Day, I have an app with 2 activities: main and details page.

When there is internet connection user can navigate from main to details page. When no internet connection he can`t do that.

The problem is: When I`m in details page and switch off wifi I would like to finish this activity, how can I implement this functionality? I have check in main activity class something like that:

 private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

It`s works fine when I start the app with internet or without that, but when I switch off the wifi at runtime it doesn`t works.

Anyway, thank you!

Vadim L.
  • 157
  • 2
  • 14
  • Simply re-call `isNetworkAvailable()`. Verify: return to the main activity and try to reopen the details one - it works. – Phantômaxx Sep 07 '15 at 11:03
  • @FrankN.Stein I would like without closing and relaunch app, I can do that, but for user it\`s must be automatically. I need some broadcast receiver I think, but I`m not sure – Vadim L. Sep 07 '15 at 11:06
  • or just check in onResume... – Shmuel Sep 07 '15 at 11:08
  • @Shmuel on resume works only if I switch off the wifi from settings directly, but If I try to switch off from drow-down menu settings it\`s doesn`t work – Vadim L. Sep 07 '15 at 11:15

3 Answers3

6

You have to Monitor for Changes in Connectivity

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.

Whenever internet state changes, your broadcast receiver will be called, and if the internet disconnects then you can handle it accordingly.

public class InternetReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {     

    if (isConnected()) 
        Log.d("NetReceiver", "Internet is connected");  
    else
        Log.d("NetReceiver", "Internet is not connected");    
   }   
};

This method checks for connections from all internet sources, including 3g

public boolean isConnected() {

Runtime runtime = Runtime.getRuntime();
try {

    Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
    int exitValue = ipProcess.waitFor();
    return (exitValue == 0);

  } catch (IOException e)          { Log.e("ERROR", "IOException",e); } 
    catch (InterruptedException e) { Log.e("ERROR", "InterruptedException",e); }

return false;
}

In your manifest file add this:

<receiver android:name=".InternetReceiver">
<intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>

Here is the Source

Community
  • 1
  • 1
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • Next step is make an instance of this class ( in details page onCreate() ) and call the isConnected() method ? – Vadim L. Sep 07 '15 at 11:25
  • No, `isConnected()` will be called itself whenever internet state is disconnected.. you don't have to do anything, thats the beauty of `BroadcastReceiver`.. It will be triggered when a particular event is called – Sharp Edge Sep 07 '15 at 11:26
3

You may want to implement a broadcast receiver listening to the Event of connection termination, so that you can immediately take action and finish the detail activity. this link may help.

Community
  • 1
  • 1
Hosein Hamedi
  • 322
  • 4
  • 16
0

Try this:

  • Create a helper method that checks internet state or connection
  • Create a Thread or Runnable
  • Inside the run() of the Thread or Runnable, call the helper method
  • In onCreate() of your MainActivity, start the Thread or Runnable to run every n sec/min
Want2bExpert
  • 527
  • 4
  • 11