如何获得Android互联网连接状态?

时间:2014-02-11 09:24:40

标签: android

如何获得HTC One应用开发的Android互联网连接状态?

我是android develpment的新手,我将开发一个带有互联网的应用程序。所以我需要获得互联网地位。任何帮助表示赞赏。

5 个答案:

答案 0 :(得分:4)

尝试这种方式和Read more

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

或其他简单的方法来实现任务

private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
    if (ni.getTypeName().equalsIgnoreCase("WIFI"))
        if (ni.isConnected())
            haveConnectedWifi = true;
    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
        if (ni.isConnected())
            haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;

}

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

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

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

在您的Android清单中。

答案 1 :(得分:0)

您可以使用以下代码获取连接状态。

public abstract class NetworkUtils 
{
    public static boolean isNetworkConnected(Context context) 
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }
}

您需要在AndroidManifest.xml文件中定义以下权限

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

现在,您可以在任何您想要的活动中使用以下代码行调用上述方法,

if (!NetworkUtils.isNetworkConnected(getApplicationContext())) 
{
    // msg to display.
}

答案 2 :(得分:0)

使用此代码:此代码适用于所有Android版本..

public static boolean isInternetOn(Context context) 
    {
         boolean haveConnectedWifi = false;
         boolean haveConnectedMobile = false;

         ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo[] netInfo = cm.getAllNetworkInfo();
         for (NetworkInfo ni : netInfo) 
         {
             if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                 if (ni.isConnected())
                     haveConnectedWifi = true;
             if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                 if (ni.isConnected())
                     haveConnectedMobile = true;
         }
         return haveConnectedWifi || haveConnectedMobile;
    }

如果互联网开启,则返回true;如果互联网不可用,则返回false。

答案 3 :(得分:0)

public static boolean isNetworkAvailable(Context context)
{
  return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}

public static boolean isNetworkAvailable(Context context) {
  boolean outcome = false;

  if (context != null) {
    ConnectivityManager cm = (ConnectivityManager) context
      .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

    for (NetworkInfo tempNetworkInfo : networkInfos) {


      /**
       * Can also check if the user is in roaming
       */
      if (tempNetworkInfo.isConnected()) {
        outcome = true;
        break;
      }
    }
  }

  return outcome;
}

不要忘记在此行中添加清单文件:

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

答案 4 :(得分:-1)

您可以按如下方式创建一个ConnectionDetector类

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

    private Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

    public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null) 
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) 
                  for (int i = 0; i < info.length; i++) 
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }
}

您可以按照以下方式使用此课程

ConnectionDetector detector= new ConnectionDetector(getApplicationContext());

Boolean isInternetPresent = detector.isConnectingToInternet(); // true or false

使用此布尔值isInternetPresent,您可以继续进行开发。

if (isInternetPresent) {
                    // Internet Connection is Present


                } else {
                    // Internet connection is not present

                }

支持将所有权限添加到manifest.xml中

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