0

I have a Button that downoad a File, and works fine. If I dont have Internet conection, the file doesnt download (obviously). The problem is that the app continues trying to download, instead of stop it.

I want to show a Toast saying "The device its not connected" or some stuff like that and NOT begin the download process then. I want a function that returns true or false if I have WIFI connection avaiable AT THE MOMENT or not

I try with the answers of these post: How to check currently internet connection is available or not in android , but the function retuns always true, even with Airplane mode.

I download the File with a DownloadManager and continues after download with a BroadcastReceiver.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • `I try with the answers of these post` which one did you try? – Zun Jul 03 '19 at 12:02
  • https://github.com/tonyofrancis/Fetch – Quick learner Jul 03 '19 at 12:04
  • OK I am thinking that the problem is how I test the code. I was testing it with the emulator, removing my laptop's wifi conections. But thats not the point. I have to remove the wifi conection manually on the emulator. Now it worked. Thanks anyway – Miwell Joestar Jul 03 '19 at 17:02

3 Answers3

0

Add this permission in Manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Use this condition to check if Wifi Connected or not

ConnectivityManager connManager = (ConnectivityManager);getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
// Do whatever
}
Mahmoud Waked
  • 357
  • 2
  • 8
0

Here's Network connectivity checking code

public class NetworUtil {
  private static NetworkInfo activeNetwork;
  public static boolean isInternetConnected(Context context) {
      if (context == null)
         return false;
      else {
          ConnectivityManager cm =(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }
 }
}

Below permission must be required in the AndroidManifest file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Sanjay Bhalani
  • 2,424
  • 18
  • 44
0

Add this permission in Manifest file

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Following condition will help you

ConnectivityManager connManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()){
    // If Wi-Fi connected
}else{
    Toast.makeText(getApplicationContext() /*Context field*/,"Please Connect to Wifi",Toast.LENGTH_SHORT).show();
}
Ashish
  • 6,791
  • 3
  • 26
  • 48