Random broadcast receiver call when wi-fi state changes in android 4.2.2

My goal is a print log when wi-fi state changes.

I am using below code.

MainActivity.java (Main activity)

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this,WiFiService.class)); } @Override protected void onStart() { super.onStart(); Log.d("Start Service", "Start Service");; startService(new Intent(this,WiFiService.class)); } } 

WiFiService.java (Service)

 public class WiFiService extends Service { WiFiBroadCasetReceiver brod; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); brod=new WiFiBroadCasetReceiver(); this.registerReceiver(brod, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)); } @Override public void onDestroy() { super.onDestroy(); } } 

WiFiBroadCasetReceiver.java (BroadcastReceiver)

 public class WiFiBroadCasetReceiver extends BroadcastReceiver { @Override public void onReceive(Context arg0, Intent arg1) { Log.d("on receiver", "receiver"); } } 

AndroidManifest.xml

 <application> ........ ........ <receiver android:name=".WiFiBroadCasetReceiver" > <intent-filter> <action android:name="android.net.wifi.supplicant.STATE_CHANGE" /> </intent-filter> </receiver> </application> 

Problem:

The code works fine in versions of android 4.0 and lower. When I change the state of wi-fi, the broadcast receiver causes random times. Thus, the magazine prints random times. I need only once. It works great, and all versions for Android remain android 4.1.0. or higher version (Jelly Bean). I am using android.net.wifi.WIFI_STATE_CHANGED. But still the same mistake.

+4
source share
2 answers

Your question should consider a few questions.

First, you can mislead the state of "android.net.wifi.supplicant.STATE_CHANGE" and "android.net.wifi.WIFI_STATE_CHANGED". And I think what you really want is later. See Comment from source.

 /** * Broadcast intent action indicating that Wi-Fi has been enabled, disabled, * enabling, disabling, or unknown. One extra provides this state as an int. * Another extra provides the previous state, if available. * * @see #EXTRA_WIFI_STATE * @see #EXTRA_PREVIOUS_WIFI_STATE */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String WIFI_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_STATE_CHANGED"; 

and

 /** * Broadcast intent action indicating that a connection to the supplicant has * been established (and it is now possible * to perform Wi-Fi operations) or the connection to the supplicant has been * lost. One extra provides the connection state as a boolean, where {@code true} * means CONNECTED. * @see #EXTRA_SUPPLICANT_CONNECTED */ @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) public static final String SUPPLICANT_CONNECTION_CHANGE_ACTION = "android.net.wifi.supplicant.CONNECTION_CHANGE"; 

Secondly, why did you receive multiple callbacks? And the number of times is random? I think you may need to carefully check your code:

 1. You start the service twice, once in Activity.onCreate() and once in Activity.onStart() 2. You register your broadcast receiver twice, once in AndroidManifest.xml and once in Service.onStart() 3. The most important thing is that you will create a new instance of your broadcast receiver instance in your Service.onStart(). That is to say, whenever your service is start, a new receiver will be created and registered. And looking back on 1, you see every time you bring you Activity back will call the service to start again. 

So, random callback times is your bad code. Just delete all broadcast registries, leaving only the one located in AndroidManifest.xml

Finally, why can't you get it to work on JellyBean later? I think this is because you did not indicate the correct action. Try "android.net.wifi.WIFI_STATE_CHANGED" instead of "android.net.wifi.supplicant.STATE_CHANGE" and try again.

+5
source

The transmission is received whenever the status of the requesting Wi-Fi changes. Since this will change several times during the connection, multiple broadcasts are expected. (I remember that I saw this on devices 2.3 as well, but mostly I use broadcast to test the connection to check for a change in connection, so I might be wrong).

The workaround you can make is that in your broadcast receiver, check that additional features are included in the intent, which indicates the state of the provider. If the condition of the supplicant in an emergency is equal to SupplicantState.COMPLETED (Wi-Fi is connected and authenticated), then only implement your application logic, otherwise ignore the broadcast.

+4
source

Source: https://habr.com/ru/post/1495273/


All Articles