0

我已经尝试过这方面的工作并且几乎成功,但未能在三星 Duos(GT-S7392)中获得结果。我正在使用以下代码:-

boolean mobileDataEnabled = false; // Assume disabled
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
try {
    Class cmClass = Class.forName(cm.getClass().getName());
    Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
    method.setAccessible(true); // Make the method callable
    // get the setting for "mobile data"
    mobileDataEnabled = (Boolean)method.invoke(cm);
} catch (Exception e) {
    // Some problem accessible private API
    // TODO do whatever error handling you want here
}

请帮我解决三星 Duos 中的这个问题。

谢谢

4

1 回答 1

0

You can check your connectivity type with this code:

 public void checkConnectivity(Context context) {
  ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (connectivityManager != null) {
     NetworkInfo ni_act = null;
     try {
        ni_act = connectivityManager.getActiveNetworkInfo();
     }
     catch (Exception e) {
        e.printStackTrace();
     }
     if (ni_act != null) {
        // Only update if WiFi or 3G is connected and not roaming
        int netType = ni_act.getType();
        NetworkInfo.State netState = ni_act.getState();
        int netSubtype = ni_act.getSubtype();

        if (netState == NetworkInfo.State.CONNECTED) {
           postHandlers(NETWORK_CONNECTED);
           if (netType == ConnectivityManager.TYPE_WIFI) {
              postHandlers(NETWORK_CONNECTED_WIFI);
           }

           if (netType == ConnectivityManager.TYPE_MOBILE) {
              // 3G (or better)
              if (netSubtype >= TelephonyManager.NETWORK_TYPE_UMTS) {
                 postHandlers(NETWORK_CONNECTED_UMTS);
              }
              // GPRS (or EDGE)
              if (netSubtype == TelephonyManager.NETWORK_TYPE_GPRS || netSubtype == TelephonyManager.NETWORK_TYPE_EDGE) {
                 postHandlers(NETWORK_CONNECTED_EDGE);
              }
              // UNKNOWN
              if (netSubtype == TelephonyManager.NETWORK_TYPE_UNKNOWN) {
                 postHandlers(NETWORK_CONNECTED_UNKNOWN);
              }
           }

        }
     }
     else {
        NetworkInfo ni_cell = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo ni_wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if ((ni_cell != null && ni_cell.getState() == NetworkInfo.State.DISCONNECTED) || (ni_wifi != null && ni_wifi.getState() == NetworkInfo.State.DISCONNECTED))
           postHandlers(NETWORK_DISCONNECTED);
     }
  }

}
于 2015-02-03T12:18:58.130 回答