English 中文(简体)
Retrieve wifilink status(Android)
原标题:Retrieve wifi connection status(Android)
最佳回答

登记:

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");
    }
  }
问题回答

不使用

NetworkInfo info = intent.getParcelableExtra(ConnectivityManager
                                                           .EXTRA_NETWORK_INFO);

提炼

int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
     WifiManager.WIFI_STATE_UNKNOWN);

rel=“nofollow”>

该法典规定:

IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiverWifi, mIntentFilter);


class WifiReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action  = intent.getAction();
        if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
            Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
            SupplicantState supl_state=((SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
            switch(supl_state){
            case ASSOCIATED:Log.i("SupplicantState", "ASSOCIATED");
                break;
            case ASSOCIATING:Log.i("SupplicantState", "ASSOCIATING");
                break;
            case AUTHENTICATING:Log.i("SupplicantState", "Authenticating...");
                break;
            case COMPLETED:Log.i("SupplicantState", "Connected");
                break;
            case DISCONNECTED:Log.i("SupplicantState", "Disconnected");
                break;
            case DORMANT:Log.i("SupplicantState", "DORMANT");
                break;
            case FOUR_WAY_HANDSHAKE:Log.i("SupplicantState", "FOUR_WAY_HANDSHAKE");
                break;
            case GROUP_HANDSHAKE:Log.i("SupplicantState", "GROUP_HANDSHAKE");
                break;
            case INACTIVE:Log.i("SupplicantState", "INACTIVE");
                break;
            case INTERFACE_DISABLED:Log.i("SupplicantState", "INTERFACE_DISABLED");
                break;
            case INVALID:Log.i("SupplicantState", "INVALID");
                break;
            case SCANNING:Log.i("SupplicantState", "SCANNING");
                break;
            case UNINITIALIZED:Log.i("SupplicantState", "UNINITIALIZED");
                break;
            default:Log.i("SupplicantState", "Unknown");
                break;

            }
            int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if(supl_error==WifiManager.ERROR_AUTHENTICATING){
                Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            }
        }
    }
}

<receiver
    android:name=".MyActivity$WifiReceiver"
    android:process=":remote" >
</receiver>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...