如何检查 Honeycomb 上的网络可用性?

发布于 2024-12-18 05:10:20 字数 688 浏览 3 评论 0原文

我正在尝试检查我的蜂窝应用程序上是否有可用的简单连接。我已经尝试了该函数的 2 个不同版本:

    public boolean networkAvailable() {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }

尽管

    private boolean networkAvailable(){
    ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectionManager.getActiveNetworkInfo();

    return activeNetwork != null;
}

这些版本在我的 Honeycomb 平板电脑上都不起作用,但无论我尝试调用它们,它们都会崩溃。在 3.2 上有没有一种特殊的方法可以做到这一点,或者......我错过了什么?

I'm trying to check if a simple connection is available on my honeycomb app. I've tried 2 different versions of the function:

    public boolean networkAvailable() {
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
     return cm.getActiveNetworkInfo().isConnectedOrConnecting();
    }

and

    private boolean networkAvailable(){
    ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = connectionManager.getActiveNetworkInfo();

    return activeNetwork != null;
}

Neither of these work on my Honeycomb tablet though, they crash wherever I try to call them. Is there a particular way of doing this on 3.2 or...what am I missing?

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

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

发布评论

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

评论(2

小巷里的女流氓 2024-12-25 05:10:20

这应该有效。您甚至可以区分可用网络的类型。确保您拥有清单中声明的​​以下权限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

public void checkkStatus()
 {
 final ConnectivityManager connMgr = (ConnectivityManager)
 this.getSystemService(Context.CONNECTIVITY_SERVICE);

 final android.net.NetworkInfo wifi =
 connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

 final android.net.NetworkInfo mobile =
 connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

 if( wifi.isAvailable() ){
 Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
 }
 else if( mobile.isAvailable() ){
 Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
 }
 else
 {Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
 }

This should work. You can even differentiate the type of network available. Make sure you have the following permissions declared in the manifest.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

public void checkkStatus()
 {
 final ConnectivityManager connMgr = (ConnectivityManager)
 this.getSystemService(Context.CONNECTIVITY_SERVICE);

 final android.net.NetworkInfo wifi =
 connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

 final android.net.NetworkInfo mobile =
 connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

 if( wifi.isAvailable() ){
 Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
 }
 else if( mobile.isAvailable() ){
 Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
 }
 else
 {Toast.makeText(this, "No Network " , Toast.LENGTH_LONG).show();}
 }
你怎么这么可爱啊 2024-12-25 05:10:20

试试这个:

   public boolean isOnline() {
         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         return cm.getActiveNetworkInfo().isConnectedOrConnecting();
   }

Try This :

   public boolean isOnline() {
         ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
         return cm.getActiveNetworkInfo().isConnectedOrConnecting();
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文