2013-06-12 50 views

回答

0

你可以管理連接:

public class InternetManager extends BroadcastReceiver { 
/** 
* PRIVATE ATTRIBUTES 
*/ 
private Context  _context; 
private boolean  _noConnectivity; 
private String  _reason; 
private boolean  _isFailover; 
private NetworkInfo _currentNetworkInfo; 
private NetworkInfo _otherNetworkInfo; 

/** 
* Constructeur 
* 
* @param context 
*  (this, activity...) 
*/ 
public InternetManager(Context context) { 

    this._context = context; 
} 

/** 
* Methode qui retourne true si le telephone est connecte a Internet sinon false 
* 
* @return 
*/ 
public boolean isConnected() { 

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

public static NetworkInfo getNetworkInfo(Context context) { 

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    return cm.getActiveNetworkInfo(); 
} 

public static boolean isConnected(Context context) { 

    NetworkInfo info = getNetworkInfo(context); 
    return (info != null && info.isConnected()); 
} 

public static boolean isConnectedWifi(Context context) { 

    NetworkInfo info = getNetworkInfo(context); 
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI); 
} 

public static boolean isConnectedMobile(Context context) { 

    NetworkInfo info = getNetworkInfo(context); 
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE); 
} 

public static boolean isConnectedFast(Context context) { 

    NetworkInfo info = getNetworkInfo(context); 
    return (info != null && info.isConnected() && isConnectionFast(info.getType(), info.getSubtype())); 
} 

private static boolean isConnectionFast(int type, int subType) { 

    if (type == ConnectivityManager.TYPE_WIFI) { 
     return true; 
    } 
    else if (type == ConnectivityManager.TYPE_MOBILE) { 
     switch (subType) { 
      case TelephonyManager.NETWORK_TYPE_1xRTT: 
       return false; // ~ 50-100 kbps 
      case TelephonyManager.NETWORK_TYPE_CDMA: 
       return false; // ~ 14-64 kbps 
      case TelephonyManager.NETWORK_TYPE_EDGE: 
       return false; // ~ 50-100 kbps 
      case TelephonyManager.NETWORK_TYPE_EVDO_0: 
       return true; // ~ 400-1000 kbps 
      case TelephonyManager.NETWORK_TYPE_EVDO_A: 
       return true; // ~ 600-1400 kbps 
      case TelephonyManager.NETWORK_TYPE_GPRS: 
       return false; // ~ 100 kbps 
      case TelephonyManager.NETWORK_TYPE_HSDPA: 
       return true; // ~ 2-14 Mbps 
      case TelephonyManager.NETWORK_TYPE_HSPA: 
       return true; // ~ 700-1700 kbps 
      case TelephonyManager.NETWORK_TYPE_HSUPA: 
       return true; // ~ 1-23 Mbps 
      case TelephonyManager.NETWORK_TYPE_UMTS: 
       return true; // ~ 400-7000 kbps 
      case TelephonyManager.NETWORK_TYPE_UNKNOWN: 
      default: 
       return false; 
     } 
    } 
    else { 
     return false; 
    } 
} 

/** 
* Enregistre l'observateur de connectivité 
*/ 
public void registerReceivers() { 

    _context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); 
} 

/** 
* Observateur 
*/ 
@Override 
public void onReceive(Context context, Intent intent) { 

    ConnectivityManager cm = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    TelephonyManager tm = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE); 
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
    // detail 
    _noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
    _reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); 
    _isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); 
    _currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO); 
    _otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO); 
    if (_noConnectivity) { 
     Toast.makeText(_context, "No connectivity", Toast.LENGTH_SHORT).show(); 
    } 
    else { 
     if (isConnectedFast(_context)) { 
      Toast.makeText(_context, "Good connexion", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      Toast.makeText(_context, "Bad connexion", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
} 
相關問題