Good day to all!
In my program, the onReceive method in the StartServiceReceiver receiver, triggered by the launch of the program (or by the screen rotation), the message "android.net.wifi.STATE_CHANGE" comes in, how can this be?

 public class KsivaFTPApacheActivity extends Activity { Context context = null; BroadcastReceiver StartService; BroadcastReceiver GetStatus; private final IntentFilter StartServiceFilter = new IntentFilter(); private final IntentFilter GetStatusFilter = new IntentFilter(); public static final String CUSTOM_INTENT = "com.imholynx.KsivaFTPApache.intent.start"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); StartServiceFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); StartServiceFilter.addAction(CUSTOM_INTENT); StartService = new StartServiceReceiver(); GetStatusFilter.addAction("AppService"); GetStatus = new GetStatusReceiver(); this.registerReceiver(StartService, StartServiceFilter); this.registerReceiver(GetStatus, GetStatusFilter); } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override protected void onDestroy() { super.onDestroy(); this.unregisterReceiver(StartService); this.unregisterReceiver(GetStatus); stopService(new Intent(this, KsivaFTPApacheService.class)); } public class StartServiceReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("State", intent.getAction()); Bundle b = intent.getExtras(); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); { NetworkInfo ni = null; ni = cm.getActiveNetworkInfo(); if (ni != null) if (ni.isConnected()) { Intent inten; Log.i("INFO", ((Integer) Files.size()).toString()); if (!Files.isEmpty()) for (int i = 0; i < Files.size(); i++) { if (Files.elementAt(i).Status == false) { Log.i("INFO","send "+ Files.elementAt(i).FileName); inten = new Intent(context,KsivaFTPApacheService.class); Log.i("INFO", ((Integer) Files.size()).toString()); Log.i("INFO2", ((Integer) i).toString()); inten.putExtra("FileName",Files.elementAt(i).FileName); inten.putExtra("Action",Files.elementAt(i).Action); inten.putExtra("ID", i); context.startService(inten); } } } } } } public class GetStatusReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("AppService")) { Log.i("AppService", ((Integer) intent.getIntExtra("Data", 1)).toString()); Files.elementAt(intent.getIntExtra("Data", 1)).Status = true; } } } 

}

    3 answers 3

    And what else do you want? You have a Broadcast filter that is configured to change the WifiManager.NETWORK_STATE_CHANGED_ACTION connection WifiManager.NETWORK_STATE_CHANGED_ACTION - naturally the intent of this will come in the Broadcast receiver.

    PS Pay attention to violations in your Java naming convention code. In particular, the StartServiceFilter variable, should be called startServiceFilter - a trifle like, but it cuts its eyes specifically.

    • So I expect it to come when the status of wifi changes. And it also comes when you start the program and when you turn the screen ... - imholynx
    • I did not understand the question then - can I explain in more detail what you want? - Barmaley
    • I'll try in order. In this program, I run the program and immediately comes android.net.wifi.STATE_CHANGE I turn the phone and again comes android.net.wifi.STATE_CHANGE At the same time, the wifi status on the phone does not change - imholynx
    • Well, how would you add StartServiceFilter.addAction (CUSTOM_INTENT); So nothing surprising - rasmisha

    so that when the screen is rotated onReceive (), add the following to the activation manifest:

     android:configChanges="keyboardHidden|orientation" 

    a if the Android version is higher than 3, then you need to add another screenSize

    This line says that you yourself will handle the change of orientation of the device (in fact, you will not;))

    • Thank you very much, a little late, but now I will know. - imholynx

    I decided to return to the question that I asked a year ago and which I could not find a solution.


    In the Android OS, there is such a type of Intention as Sticky Intents (sticky). The main difference between them and ordinary Intents is that ordinary ones come at the moment of sending, and sticky ones can be obtained at any time when registering a receiver.

    In my case, when registering the receiver, the sticky Intent immediately came to it, which kept the last change that happened with WiFi. It was the arrival of this Intent that I could not explain.

    You can find out whether the Intent is sticky in the onReceive () receiver using the isInitialStickyBroadcast () method:

     public class StartServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(!isInitialStickyBroadcast()){ // Реагируем только на не липкие Интенты } } } 

    I hope that someone will help my answer. Well, I enjoyed the fact that I found a solution.