代码在 wifi 关闭和 3G 打开时有效,但在 wifi 关闭和 4G 时无效

Code work when wifi off and 3G on but not when wifioff and 4G

我所做的是

我正在用 wifi 播放流,3g 也打开了。 我禁用了 wifi,流停止但立即重新启动,因为 3g 已启用。

但是当 wifi 和 4g 打开时问题就开始了,那时它不工作

我添加了像

这样的监听器
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver,intentFilter) ;   

接收者是

BroadcastReceiver receiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {



            try{  {
                if(GetNetworkStatus.isNetworkAvailable(getApplicationContext()))
                   {if(!mIsPlaying)
                    justPlay();

                   }
                else
                {justStop();

                 handler=new Handler();

                    r = new Runnable()
                    {
                        public void run() 
                        {

                            try{
                                Thread.sleep(4000);
                                }
                                catch(Exception s)
                                {}
                            if(GetNetworkStatus.isNetworkAvailable(getApplicationContext()))
                            justPlay();
                            /*if(!mIsPlaying)
                            handler.postDelayed(this, 3000);*/
                        }
                    };
                    try{
                         handler.postDelayed(r, 1000);

                     }
                     catch(Exception e)
                     {

                     }

                Toast.makeText(getApplicationContext(), "Internet Connection Failed", Toast.LENGTH_LONG).show();
                }
            }
            }
            catch(Exception e)
            {
                finish();
            } }
        };

我相信记录 Extra 内容信息,可能会帮助您确定实际原因。 参考 url:http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION

A change in network connectivity has occurred. A default connection has either been established or lost. The NetworkInfo for the affected network is sent as an extra; it should be consulted to see what kind of connectivity event occurred.

If this is a connection that was the result of failing over from a disconnected network, then the FAILOVER_CONNECTION boolean extra is set to true.

For a loss of connectivity, if the connectivity manager is attempting to connect (or has already connected) to another network, the NetworkInfo for the new network is also passed as an extra. This lets any receivers of the broadcast know that they should not necessarily tell the user that no data traffic will be possible. Instead, the receiver should expect another broadcast soon, indicating either that the failover attempt succeeded (and so there is still overall data connectivity), or that the failover attempt failed, meaning that all connectivity has been lost.

For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY is set to true if there are no connected networks at all.

Constant Value: "android.net.conn.CONNECTIVITY_CHANGE"

在您的 GetNetworkStatus.isNetworkAvailable() 中,您是否正确检查了 LTE 连接类型? 您可以使用以下方法(从其他地方收集但效果很好)

public static String getNetworkClass(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if(info==null || !info.isConnected())
        return "-"; //not connected
    if(info.getType() == ConnectivityManager.TYPE_WIFI)
        return "WIFI";
    if(info.getType() == ConnectivityManager.TYPE_MOBILE){
        int networkType = info.getSubtype();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return "2G";
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return "3G";
            case TelephonyManager.NETWORK_TYPE_LTE:
                return "4G";
            default:
                return "?";
         }
    }
    return "?";
}