如何在检测到wifi时检测Android应用程序

时间:2014-07-09 05:52:28

标签: android wifi

我希望我的应用能够检测到连接的无线网络。

例如,我想知道应用程序如何连接到wifi并告诉我它已经连接。

1 个答案:

答案 0 :(得分:0)

public class WiFiChangeBroadcastReceiver extends BroadcastReceiver {

    private String LOGTAG = getClass().getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(LOGTAG, "WiFi Status Changed");
        if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo networkInfo = intent
                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (networkInfo.isConnected()) {
                Log.d(LOGTAG,
                        "Wifi is connected: " + String.valueOf(networkInfo));
            }
        }
    }
}

Android Manifest:

    <receiver android:name=".WiFiChangeBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.STATE_CHANGE" />
        </intent-filter>
    </receiver>