请求方连接更改不会触发
supplicant connection change doesn't trigger
我想在 wifi 连接建立后做点什么。
我有一个 BroadcastReceiver 可以完美地接收 NETWORK_STATE_CHANGED_ACTION 和 SCAN_RESULTS_AVAILABLE_ACTION,但不能接收 SUPPLICANT_CONNECTION_CHANGE_ACTION。这个更难测试:我为此打开 off/on 路由器。
protected void onCreate(Bundle savedInstanceState) {
receiverWifi = new WifiReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverWifi, intentFilter);
//...
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();
Log.d("mhp","*BroadcastReceiver: " + action")}
还有 Manifiest.xml
<application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
和AndroidManifest.xml:
<application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
连接wifi网络,即使你没有得到SUPPLICANT_CONNECTION_CHANGE_ACTION
,你肯定会得到NETWORK_STATE_CHANGED_ACTION
,你可以玩这个动作来满足你的所有需要。
在广播接收器中,执行此操作:
String action = intent.getAction();
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action))
{
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if ( (netInfo.getDetailedState()==(NetworkInfo.DetailedState.CONNECTED)) )
{
// your wifi is connected, do what you want to do
}
}
我想在 wifi 连接建立后做点什么。 我有一个 BroadcastReceiver 可以完美地接收 NETWORK_STATE_CHANGED_ACTION 和 SCAN_RESULTS_AVAILABLE_ACTION,但不能接收 SUPPLICANT_CONNECTION_CHANGE_ACTION。这个更难测试:我为此打开 off/on 路由器。
protected void onCreate(Bundle savedInstanceState) {
receiverWifi = new WifiReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverWifi, intentFilter);
//...
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();
Log.d("mhp","*BroadcastReceiver: " + action")}
还有 Manifiest.xml
<application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
和AndroidManifest.xml:
<application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
连接wifi网络,即使你没有得到SUPPLICANT_CONNECTION_CHANGE_ACTION
,你肯定会得到NETWORK_STATE_CHANGED_ACTION
,你可以玩这个动作来满足你的所有需要。
在广播接收器中,执行此操作:
String action = intent.getAction();
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action))
{
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if ( (netInfo.getDetailedState()==(NetworkInfo.DetailedState.CONNECTED)) )
{
// your wifi is connected, do what you want to do
}
}