Internet connection status

magarcan

Active Member
Licensed User
Longtime User
Hi. I need to know the Internet connection status, before downloading some files.
Using phone GetDataState I know if there is 3G connection...

Are there any way to know if am I connected using 3G or wifi?? Thanks!!
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
You can follow Erel's video tutorial on how to setup your eclipse platform and make a library.
When you have done settings the first thing up, you can copy the following code and paste in your eclipse project:

I tested this code some time ago: it should work, but i'm not sure about wifi though.
B4X:
public boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) ba.applicationcontext.getSystemService(ba.applicationcontext.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

Don't forget to add "android.permission.ACCESS_NETWORK_STATE" permission


Or this code:

B4X:
public boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) ba.applicationcontext.getSystemService(ba.applicationcontext.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

You can make a library to your needs, for example custom library and everytime you need an new extension, you can edit and add new code to the library.

Tomas
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
Can't this be done without a library? I do not care if you have a 3G connection or WiFi. I only want to know if there is Internet connection...

Thanks!!!
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
Sample code:
B4X:
Sub CheckConnection
   Dim p As Phone
   Dim connected As Boolean
   connected = False
   
   If (p.GetDataState == "CONNECTED") Then
      connected = True
   End If
   
   If (p.GetSettings ("wifi_on") == 1) Then
      connected = True
   End If
   
'   If connected == True Then 
'      ToastMessageShow("INTERNET", True)
'   Else 
'      ToastMessageShow("NO INTERNET", True)
'   End If
   
   Return connected
End Sub
Testing it :cool:
 
Upvote 0
Top