2012-08-03 68 views
2

嗨,我是利用android機器人連接

public boolean isOnline() { 
    ConnectivityManager connMgr = (ConnectivityManager) 
    getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    return (networkInfo != null && networkInfo.isConnected()); 
} 

這個片段但當我打斷從我的桌面互聯網連接也不會發現,未連接,並嘗試connect.I'm在adnroid模擬器中運行它。我的問題:爲什麼它不會返回false並且仍然指出它已連接?如果它通過3D連接,它不應該獲取數據嗎?

+0

你不使用xmpp連接... – shassss 2012-08-03 10:38:51

+0

好吧,我其實只是在android的第一步。你在說什麼? – Libathos 2012-08-03 10:41:48

回答

0
public class Networking { 
/* 
*@return boolean return true if the application can access the internet 
*/ 
public static boolean isNetworkAvailable(Context context) { 
    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; 
    } 
} 
1

禁用計算機上的互聯網連接不會反映模擬器上的更改。

您將需要直接從仿真器禁用Internet,就像您真正的手機上做的:

Settings -> Wireless and networks -> Mobile Networks -> Use packed data.

(我猜的路徑可以從模擬器具體費用取決於的SDK版本的仿真器)

0
 ConnectivityManager connMgr = 
       (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     // Checks the user prefs and the network connection. Based on the result, decides 
     // whether 
     // to refresh the display or keep the current display. 
     // If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection. 
     if (WIFI.equals(sPref) && networkInfo != null 
       && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
      // If device has its Wi-Fi connection, sets refreshDisplay 
      // to true. This causes the display to be refreshed when the user 
      // returns to the app. 
      refreshDisplay = true; 
      Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show(); 

      // If the setting is ANY network and there is a network connection 
      // (which by process of elimination would be mobile), sets refreshDisplay to true. 
     } else if (ANY.equals(sPref) && networkInfo != null) { 
      refreshDisplay = true; 

      // Otherwise, the app can't download content--either because there is no network 
      // connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there 
      // is no Wi-Fi connection. 
      // Sets refreshDisplay to false. 
     } else { 
      refreshDisplay = false; 
      Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show(); 
     } 
1

公共類HttpManager {

public static String getData(String uri) { 

    BufferedReader reader = null; 

    try { 
     URL url = new URL(uri); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

     StringBuilder sb = new StringBuilder(); 
     reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

     String line; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     return sb.toString(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 

}