本文介绍了检索无线网络连接状态(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我按照following链接与本地的无线网络连接。这是工作完美。我能够连接到我本地的无线网络。

不过,我真正想要的无线网络的当前状态,我使用下面的code以获得无线的状态。但不幸的是,我只能得到禁用,已启用和扫描的状态,我真的想收到其他中间状态。

你能告诉我什么,我做错了。

 公共类ConnectionChangeReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        的NetworkInfo信息= intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        消息味精=新的Message();
        msg.what = 5;
        叠B =新包();
        字符串sStatus =未知;
        开关(info.getDetailedState()){
        案例验证:
            sStatus =身份验证...;
            打破;
        案件有关:
            sStatus =已连接;
            打破;
        案件正在连接:
            sStatus =连接...;
            打破;
        案例DISCONNECTED:
            sStatus =断开连接;
            打破;
        案例DISCONNECTING:
            sStatus =断开连接...;
            打破;
        例失败:
            sStatus =失败;
            打破;
        空闲的情况下:
            sStatus =空闲;
            打破;
        案例OBTAINING_IPADDR:
            sStatus =获取IP地址...;
            打破;
        案例扫描:
            sStatus =扫描...;
            打破;
        案例悬挂:
            sStatus =暂停;
            打破;
        }
        b.putString(状态,sStatus);
        msg.setData(B);
        mHandler.sendMessage(MSG);
    }
}
 

予登记以下列方式的广播接收器

  IntentFilter的过滤器=新的IntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkStateReceiver,过滤器);
 

解决方案

注册reciver为::

  mIntentFilter =新的IntentFilter();
  mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  registerReceiver(mReceiver,mIntentFilter);




公共无效的onReceive(上下文的背景下,意图意图)
   {
    串动= intent.getAction();
    如果(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
    {
    }否则,如果(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
    {
     INT ITEMP = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
       WifiManager.WIFI_STATE_UNKNOWN);
     LogUtil.d(LOG_SET,+++++++ ----------- ------ wifiStateReceiver +++++++,DEBUG);
     的CheckState(ITEMP);
    }否则,如果(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
    {
     DetailedState状态= WifiInfo.getDetailedStateOf((SupplicantState)
     intent.getParcelableExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED));
     改变状态(状态);
     LogUtil.d(LOG_SET,------------>>>> SUPPLICANT_STATE_CHANGED_ACTION<<<<<< -------,DEBUG) ;
    }
    否则,如果(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
    {
        DetailedState状态=
       ((的NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO))getDetailedState();
       改变状态(状态);
     }
   }
   私人无效改变状态(DetailedState aState)
    {
     LogUtil.d(LOG_SET, ">>>>>>>>>>>>>>>>>>changeState<<<<<<<<<<<<<<<<"+aState, DEBUG);
   如果(aState == DetailedState.SCANNING)
   {
   Log.d(wifiSupplicanState,扫描);
   }
   否则,如果(aState == DetailedState.CONNECTING)
   {
   Log.d(wifiSupplicanState,连接);
   }否则,如果(aState == DetailedState.OBTAINING_IPADDR)
   {
   Log.d(wifiSupplicanState,OBTAINING_IPADDR);
   }
   否则,如果(aState == DetailedState.CONNECTED)
   {
   Log.d(wifiSupplicanState,连接);
   }
   否则,如果(aState == DetailedState.DISCONNECTING)
   {
    Log.d(wifiSupplicanState,断开);
   }
   否则,如果(aState == DetailedState.DISCONNECTED)
   {
    Log.d(wifiSupplicanState,DISCONNECTTED);
   }
   否则,如果(aState == DetailedState.FAILED)
   {
   }
  }

  公共无效的CheckState(INT是不是)
  {
   LogUtil.d(LOG_SET,"==>>>>>>>>checkState<<<<<<<<"+aInt, DEBUG);
   如果(是不是== WifiManager.WIFI_STATE_ENABLING)
    {
    Log.d(WifiManager,WIFI_STATE_ENABLING);
    }
   否则,如果(是不是== WifiManager.WIFI_STATE_ENABLED)
   {
   Log.d(WifiManager,WIFI_STATE_ENABLED);
    }
    否则,如果(是不是== WifiManager.WIFI_STATE_DISABLING)
    {
    Log.d(WifiManager,WIFI_STATE_DISABLING);
    }
    否则,如果(是不是== WifiManager.WIFI_STATE_DISABLED)
    {
    Log.d(WifiManager,WIFI_STATE_DISABLED);
    }
  }
 

I have followed the following link to connect with local wifi. It is working perfect. I am able to connect to my local wifi.

But, I really want the current status of the wifi, I am using following code to get the status of wifi. But unfortunately I am only able to get 'DISABLED', 'ENABLED' and 'SCANNING' status, I really want to receive other intermediary states.

Can you tell me what I am doing wrong.

public class ConnectionChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        Message msg = new Message();
        msg.what = 5;
        Bundle b = new Bundle();
        String sStatus = "UnKnown";
        switch(info.getDetailedState()){
        case AUTHENTICATING:
            sStatus = "Authenticating...";
            break;
        case    CONNECTED:
            sStatus = "Connected";
            break;
        case    CONNECTING:
            sStatus = "Connecting...";
            break;
        case    DISCONNECTED:
            sStatus = "Disconnected";
            break;
        case    DISCONNECTING:
            sStatus = "Disconnecting...";
            break;
        case    FAILED:
            sStatus = "Failed";
            break;
        case    IDLE:
            sStatus = "Idle";
            break;
        case    OBTAINING_IPADDR :
            sStatus = "Obtaining IP Address...";
            break;
        case    SCANNING:
            sStatus = "Scanning...";
            break;
        case    SUSPENDED:
            sStatus = "Suspended";
            break;
        }
        b.putString("status", sStatus);
        msg.setData(b);
        mHandler.sendMessage(msg);
    }
}

I register the broadcast receiver in the following way

IntentFilter filter = new IntentFilter(
        ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(networkStateReceiver, filter);
解决方案

register a reciver for::

mIntentFilter = new IntentFilter();
  mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
  mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
  mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
  registerReceiver(mReceiver, mIntentFilter);




public void onReceive(Context context, Intent intent)
   {
    String action  = intent.getAction();
    if(action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION))
    {
    }else if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
    {
     int iTemp = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
       WifiManager.WIFI_STATE_UNKNOWN);
     LogUtil.d(LOG_SET, "+++++++-----------wifiStateReceiver------+++++++", DEBUG);
     checkState(iTemp);
    }else if(action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION))
    {
     DetailedState state =WifiInfo.getDetailedStateOf((SupplicantState)
     intent.getParcelableExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED));
     changeState(state);
     LogUtil.d(LOG_SET, "------------>>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<-------", DEBUG);
    }
    else if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
    {
        DetailedState state=
       ((NetworkInfo)intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).getDetailedState();
       changeState(state);
     }
   }
   private void changeState(DetailedState aState)
    {
     LogUtil.d(LOG_SET, ">>>>>>>>>>>>>>>>>>changeState<<<<<<<<<<<<<<<<"+aState, DEBUG);
   if (aState == DetailedState.SCANNING)
   {
   Log.d("wifiSupplicanState", "SCANNING");
   }
   else if (aState == DetailedState.CONNECTING)
   {
   Log.d("wifiSupplicanState", "CONNECTING");
   }else if(aState == DetailedState.OBTAINING_IPADDR)
   {
   Log.d("wifiSupplicanState", "OBTAINING_IPADDR");
   }
   else if (aState == DetailedState.CONNECTED)
   {
   Log.d("wifiSupplicanState", "CONNECTED");
   }
   else if (aState == DetailedState.DISCONNECTING)
   {
    Log.d("wifiSupplicanState", "DISCONNECTING");
   }
   else if (aState == DetailedState.DISCONNECTED)
   {
    Log.d("wifiSupplicanState", "DISCONNECTTED");
   }
   else if (aState == DetailedState.FAILED)
   {
   }
  }

  public void checkState(int aInt)
  {
   LogUtil.d(LOG_SET,"==>>>>>>>>checkState<<<<<<<<"+aInt, DEBUG);
   if (aInt == WifiManager.WIFI_STATE_ENABLING)
    {
    Log.d("WifiManager", "WIFI_STATE_ENABLING");
    }
   else if (aInt== WifiManager.WIFI_STATE_ENABLED)
   {
   Log.d("WifiManager", "WIFI_STATE_ENABLED");
    }
    else if (aInt == WifiManager.WIFI_STATE_DISABLING)
    {
    Log.d("WifiManager", "WIFI_STATE_DISABLING");
    }
    else if (aInt == WifiManager.WIFI_STATE_DISABLED)
    {
    Log.d("WifiManager", "WIFI_STATE_DISABLED");
    }
  }

这篇关于检索无线网络连接状态(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:33