Manifest:
<receiver android:name=".triggers.WifiTrigger">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
Permissions :
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
The receiver is executed when WiFi is disabled, connected or disconnected (didn't check for connecting/disabling/enabling) but it is not triggered when WiFi is enabled, in addition WifiManager.EXTRA_WIFI_STATE
extra is not available in the intent, i am using this :
public class WifiTrigger extends BroadcastReceiver {
...
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context);
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
int previousWifiState = intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
log("p"+previousWifiState);
log("c"+wifiState);
//log is a defined method used for logging (it just calls Log.d(...,...)
}
...
}
and it keeps logging p4
c4
(4 for WifiManager.WIFI_STATE_UNKNOWN)