2014-02-11 50 views
3

如何獲得HTC One應用程序開發的Android互聯網連接狀態?如何獲取android的互聯網連接狀態?

我是新開發的android開發人員,我將開發一個應用程序與互聯網。所以我需要獲得interent狀態。任何幫助讚賞。

+0

http://stackoverflow.com/a/10617567/1659523檢查此 –

回答

3

嘗試這種方式和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清單。

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. 
} 
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。

-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" /> 
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" />