0

To get whether the device has internet connection at the moment I put my the code of check_internet_connection() in BroadcastReceiver. The problem I need this method to retun boolean value but How can I manage it?

The current errors are these lines "return true" or "return false": "Void methods cannot return a value"

 public boolean check_internet_connection() {
  registerReceiver(new BroadcastReceiver() {

   @Override
   public void onReceive(Context context, Intent intent) {
    ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
      .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
     return true;
    }

    NetworkInfo mobileNetwork = cm
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
     return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
     return true;
    }
    return false;
   }

  }, new IntentFilter(ConnectivityManager.EXTRA_NETWORK_TYPE));
 }
Mr Asker
  • 2,300
  • 11
  • 31
  • 56

1 Answers1

0
public static boolean hasInternetConnection(Context context) {
        ConnectivityManager check = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

//getActiveNetworkInfo() Returns details about the currently active default data network.
    NetworkInfo info = check.getActiveNetworkInfo();

    return info != null && info.isConnected();
}

hope it helps

  • possible duplicate of http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app – mean_machine Apr 06 '15 at 16:15