【问题标题】:How to check Internet connectivity in my Google map application (Android)如何在我的 Google 地图应用程序 (Android) 中检查 Internet 连接
【发布时间】:2014-10-22 12:52:49
【问题描述】:

如何在谷歌地图上进行网络验证,它在互联网开启或WiFi开启时工作良好。但在互联网关闭时不起作用

【问题讨论】:

  • 首先你检查网络连接,如果这里连接而不是打开活动,那么打开对话框你没有网络连接也有代码来检查网络连接
  • 我已经在我的另一个项目中检查互联网连接和 WiFi 状态,但不知道如何在谷歌地图中进行操作
  • 检查网络是否比打开一些 Toast 网络不在这里

标签: android google-maps google-maps-api-2


【解决方案1】:

请试试这个。希望对你有帮助。

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI; 

        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    } 
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多