Question

I am using VideoView to play video. I need to handle internen lose connection while playing video. Currently, when device lose internet connection during playing video - player freezes, and nothing else happens. Thank you!

Était-ce utile?

La solution

You should implement an inner class extending BroadcastReceiver that will respond to "android.net.conn.CONNECTIVITY_CHANGE" and show a dialog, toast or whatever you choose.

See the official documentation on handling the connection change here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges .

Check this link for a sample code: http://thiranjith.com/2011/03/31/how-to-monitor-network-connectivity-in-android/

Best Regards

Autres conseils

Your can manage connectivity :

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();
        }
    }
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top