Sivaji Vithanala
Sivaji Vithanala

Reputation: 119

How to check if data connected or not in Android and ios using codenameOne

The following code is working fine for Android Devices bt it doesn't works in ios using codenameOne.

public static boolean checkNetwork(){
    boolean online = false;
    String net = NetworkManager.getInstance().getCurrentAccessPoint();
    if (net == null || net == "" || net.equals(null)) {
        online = false;
    } else {
        online = true;
    }
    return online;
}

Note : It seems No Connection Error Even data was connected to ios device

Upvotes: 1

Views: 73

Answers (1)

Diamond
Diamond

Reputation: 7483

There is a CN1 library written by LittleMonkey that you can use to check connectivity. Search for connectivity in Codename One Extensions and download it to your project.

You can check for connection like this:

if (Connectivity.isConnected()) {
    //we have some connection
} else {
    // we have no connection
}

And you can check what type of connection

ConnectionState status = Connectivity.getConnectionState();
switch (status) {
    case DISCONNECTED:
        Log.p("Disconnected");
        break;
    case WIFI:
        Log.p("On Wifi");
        break;
    case MOBILE:
        Log.p("On Mobile Data");
        break;
    default:
        //shouldn't be possible
}

Read more about this on GitHub.

Upvotes: 1

Related Questions