【问题标题】:How to check if data connected or not in Android and ios using codenameOne如何使用 codenameOne 在 Android 和 ios 中检查数据是否连接
【发布时间】:2017-05-12 18:58:16
【问题描述】:

以下代码在 Android 设备上运行良好,但在使用 codenameOne 的 ios 中不起作用。

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;
}

注意:似乎没有连接错误,即使数据已连接到 ios 设备

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    LittleMonkey 编写的 CN1 库可用于检查连接性。在 Codename One Extensions 中搜索 connectivity 并将其下载到您的项目中。

    您可以像这样检查连接:

    if (Connectivity.isConnected()) {
        //we have some connection
    } else {
        // we have no 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
    }
    

    阅读更多关于on GitHub的信息。

    【讨论】:

    • 仅供参考,您可以从 Codename One Settings -> Extensions 安装该库
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多