0

我正在使用 VideoView 播放视频。我需要在播放视频时处理内部丢失连接。目前,当设备在播放视频期间失去互联网连接时 - 播放器冻结,并且没有其他任何事情发生。谢谢!

4

2 回答 2

1

您应该实现一个扩展 BroadcastReceiver 的内部类,该类将响应“android.net.conn.CONNECTIVITY_CHANGE”并显示对话框、toast 或您选择的任何内容。

在此处查看有关处理连接更改的官方文档:http: //developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges

检查此链接以获取示例代码:http ://thiranjith.com/2011/03/31/how-to-monitor-network-connectivity-in-android/

最好的祝福

于 2013-06-12T09:31:46.633 回答
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();
        }
    }
}
}
于 2013-06-12T09:15:27.353 回答