2014-03-05 74 views
0

我正在嘗試使用isNetworkSupported(int networkType)來檢查硬件是否支持僅限WiFi模式。但它給像「的方法isNetworkSupported(INT)錯誤未定義類型ConnectivityManager無法訪問Android ConnectivityManager類中的isNetworkSupported

以下是我的代碼:

ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE); 
boolean checkStatus = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); 

請告訴我,如何可以訪問我們的活動,這裏面isNetworkSupported方法

謝謝。

+0

您可以粘貼代碼的細節 – VenushkaT

回答

0

謝謝你們!

經過一些研究和我的同事幫助我用java反射來解決這個問題 像下面一樣。

ConnectivityManager cm = (ConnectivityManager) 
this.getSystemService(Context.CONNECTIVITY_SERVICE); 

final Class<?> cmlClass = cm.getClass(); 
String status = ""; 
try { 

    final Method wifiCheckMethod = cmlClass.getMethod("isNetworkSupported", int.class); 
    boolean hasMobileNetwork = (Boolean) wifiCheckMethod.invoke(cm, ConnectivityManager.TYPE_MOBILE); 
    status = hasMobileNetwork ? "This device has mobile support model" : "This is wifi only model"; 
    Log.i(getClass().getSimpleName(), "The network status is..."+hasMobileNetwork); 

} catch (Exception ex) { 
    ex.printStackTrace(); 
    status = "Error while getting device support model"; 
} 

Toast.makeText(MainActivity.this, "Network support message.."+status, Toast.LENGTH_SHORT).show(); 
2

作爲每documentisNetworkSupported不是用於類ConnectivityManager的方法。

,如果你想查詢Internet連接狀態檢查這個

ConnectivityManager cm = 
    (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 

NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
boolean isConnected = activeNetwork != null && 
        activeNetwork.isConnectedOrConnecting(); 

但我能看到這種方法在https://android.googlesource.com/platform/frameworks/base.git/+/android-4.3_r2.1/core/java/android/net/ConnectivityManager.java並在Android Studio中還其顯示的方法。