如何检查Android手机的网络连接

发布于 2024-12-23 12:48:09 字数 494 浏览 0 评论 0原文

可能的重复:
Android - 检测是否有可用的互联网连接
如何检查移动设备中 WIFI 和 3G(数据套餐)中的网络连接启用或禁用吗?

在我的手机中,当我运行我的应用程序时,此应用程序崩溃,因为网络连接不存在。那么如何给出连接消息不在那里 喜欢。或者在你的妻子上

请帮助我

谢谢

Possible Duplicate:
Android - detect whether there is an Internet connection available
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?

In my cell when I run my app then this application is crashed because net connection is not there.so how to give the message that the connection is not there like. or on your wif

please help me

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-12-30 12:48:09

请参阅下面的代码。

ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // ARE WE CONNECTED TO THE NET
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;

        // internet connection is wroking
    } else {
        return false;
        // internet connection is not wroking
    }

See below code.

ConnectivityManager conMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    // ARE WE CONNECTED TO THE NET
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;

        // internet connection is wroking
    } else {
        return false;
        // internet connection is not wroking
    }
剩一世无双 2024-12-30 12:48:09

在您的活动中使用此方法,并在您想知道网络是否可用时调用此方法。

public boolean isInternetOn(Context ctx) {
    this.mContext = ctx;
    ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State connected = NetworkInfo.State.CONNECTED;
    State connecting = NetworkInfo.State.CONNECTING;
    State disconnected = NetworkInfo.State.DISCONNECTED;

    State info0 = Connect_Manager.getNetworkInfo(0).getState();
    State info1 = Connect_Manager.getNetworkInfo(1).getState();

    // ARE WE CONNECTED TO THE NET
    if (info0 == connected || info0 == connecting || info1 == connecting
            || info1 == connected) {

        // MESSAGE TO SCREEN FOR TESTING (IF REQ)
        // Toast.makeText(this, connectionType + " connected",
        // Toast.LENGTH_SHORT).show();
        Log.d("Internet", "Connected");
        return true;
    } else if (info0 == disconnected || info1 == disconnected) {
        Log.d("Internet", "DisConnected");
        // System.out.println("Not Connected");
        return false;
    }
    return false;
}

现在检查互联网连接,

if (isInternetOn(this))
     {

        //Do stuff          
      }
else
       {

           //show alert
        }

Use this method in your Activity and call this when you want to know whether the net is available or not.

public boolean isInternetOn(Context ctx) {
    this.mContext = ctx;
    ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    State connected = NetworkInfo.State.CONNECTED;
    State connecting = NetworkInfo.State.CONNECTING;
    State disconnected = NetworkInfo.State.DISCONNECTED;

    State info0 = Connect_Manager.getNetworkInfo(0).getState();
    State info1 = Connect_Manager.getNetworkInfo(1).getState();

    // ARE WE CONNECTED TO THE NET
    if (info0 == connected || info0 == connecting || info1 == connecting
            || info1 == connected) {

        // MESSAGE TO SCREEN FOR TESTING (IF REQ)
        // Toast.makeText(this, connectionType + " connected",
        // Toast.LENGTH_SHORT).show();
        Log.d("Internet", "Connected");
        return true;
    } else if (info0 == disconnected || info1 == disconnected) {
        Log.d("Internet", "DisConnected");
        // System.out.println("Not Connected");
        return false;
    }
    return false;
}

Now to check Internet Conncetion,

if (isInternetOn(this))
     {

        //Do stuff          
      }
else
       {

           //show alert
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文