I am receiving “android.net.wifi.WIFI_STATE_CHANGED” as soon as Registering BroadcastReceiver












0















I register a broadcast on runtime to receive "android.net.wifi.WIFI_STATE_CHANGED"



   IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
broadcastReceiverAction = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) {
/*I get this action as soon as calling registerReceiver(broadcastReceiverAction, intentFilter);*/
}
}
}
registerReceiver(broadcastReceiverAction, intentFilter);


Issue: This gets the broadcast action as soon as registering the receiver.
I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)










share|improve this question

























  • Possible duplicate of BroadcastReceiver onReceive triggered when registered

    – quaternion
    Nov 15 '18 at 2:59











  • I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

    – Elias Fazel
    Nov 15 '18 at 3:05
















0















I register a broadcast on runtime to receive "android.net.wifi.WIFI_STATE_CHANGED"



   IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
broadcastReceiverAction = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) {
/*I get this action as soon as calling registerReceiver(broadcastReceiverAction, intentFilter);*/
}
}
}
registerReceiver(broadcastReceiverAction, intentFilter);


Issue: This gets the broadcast action as soon as registering the receiver.
I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)










share|improve this question

























  • Possible duplicate of BroadcastReceiver onReceive triggered when registered

    – quaternion
    Nov 15 '18 at 2:59











  • I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

    – Elias Fazel
    Nov 15 '18 at 3:05














0












0








0








I register a broadcast on runtime to receive "android.net.wifi.WIFI_STATE_CHANGED"



   IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
broadcastReceiverAction = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) {
/*I get this action as soon as calling registerReceiver(broadcastReceiverAction, intentFilter);*/
}
}
}
registerReceiver(broadcastReceiverAction, intentFilter);


Issue: This gets the broadcast action as soon as registering the receiver.
I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)










share|improve this question
















I register a broadcast on runtime to receive "android.net.wifi.WIFI_STATE_CHANGED"



   IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
broadcastReceiverAction = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED")) {
/*I get this action as soon as calling registerReceiver(broadcastReceiverAction, intentFilter);*/
}
}
}
registerReceiver(broadcastReceiverAction, intentFilter);


Issue: This gets the broadcast action as soon as registering the receiver.
I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)







android broadcastreceiver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 3:05







Elias Fazel

















asked Nov 15 '18 at 2:40









Elias FazelElias Fazel

1,07879




1,07879













  • Possible duplicate of BroadcastReceiver onReceive triggered when registered

    – quaternion
    Nov 15 '18 at 2:59











  • I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

    – Elias Fazel
    Nov 15 '18 at 3:05



















  • Possible duplicate of BroadcastReceiver onReceive triggered when registered

    – quaternion
    Nov 15 '18 at 2:59











  • I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

    – Elias Fazel
    Nov 15 '18 at 3:05

















Possible duplicate of BroadcastReceiver onReceive triggered when registered

– quaternion
Nov 15 '18 at 2:59





Possible duplicate of BroadcastReceiver onReceive triggered when registered

– quaternion
Nov 15 '18 at 2:59













I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

– Elias Fazel
Nov 15 '18 at 3:05





I got this on Android API 26 & Higher Which I can not declare "android.net.wifi.WIFI_STATE_CHANGED" in Manifest. (No Longer Supported by Android System)

– Elias Fazel
Nov 15 '18 at 3:05












2 Answers
2






active

oldest

votes


















1














To check network connectivity state change you can do it programmatically. It supports API 21+.



    public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback {
public final String TAG = ConnectionStateMonitor.class.getSimpleName();
private boolean isLost = false;


final NetworkRequest networkRequest;
Context context;
private ConnectionStateMonitor instance;
private boolean isLost = false;

private ConnectionStateMonitor() {
networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build();
}

public synchronized ConnectionStateMonitor getInstance() {
if (instance == null) {
instance = new ConnectionStateMonitor();
}
return instance;
}

public void enable(Context context) {
try {
if (context == null) return;
this.context = context;
ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
connectivityManager.registerNetworkCallback(networkRequest, this);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onAvailable(Network network) {
Log.i(TAG, "onAvailable called " +
network.toString());
if (isLost) {
isLost = false;}
// when network available called it
}


@Override
public void onLost(Network network) {
super.onLost(network);
isLost = true;
// when network lost called it
Log.i(TAG, "onLost called ");
}
}





share|improve this answer


























  • When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

    – Elias Fazel
    Nov 15 '18 at 3:49






  • 1





    please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

    – Sultan Mahmud
    Nov 15 '18 at 4:43











  • As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

    – Elias Fazel
    Nov 15 '18 at 21:56



















0














[Simple Solution]



Problem is Sticky Broadcasts which trigger at the moment of registration.
To avoid this I simply add another parameter to restrict the process.
I defined a static variable



public static boolean triggerBroadcast = false;


Then I change it to after registering the broadcast by few second delay.



registerReceiver(broadcastReceiverAction, intentFilter);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
triggerWifiBroadcast = true;
}
}, 3000);


Then onReceive() I compare intent.getAction with this



if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED") && triggerBroadcast ) {
//
}


Then you can change it to false whenever decide to unregister the broadcast receiver.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311657%2fi-am-receiving-android-net-wifi-wifi-state-changed-as-soon-as-registering-broa%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    To check network connectivity state change you can do it programmatically. It supports API 21+.



        public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback {
    public final String TAG = ConnectionStateMonitor.class.getSimpleName();
    private boolean isLost = false;


    final NetworkRequest networkRequest;
    Context context;
    private ConnectionStateMonitor instance;
    private boolean isLost = false;

    private ConnectionStateMonitor() {
    networkRequest = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build();
    }

    public synchronized ConnectionStateMonitor getInstance() {
    if (instance == null) {
    instance = new ConnectionStateMonitor();
    }
    return instance;
    }

    public void enable(Context context) {
    try {
    if (context == null) return;
    this.context = context;
    ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.registerNetworkCallback(networkRequest, this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onAvailable(Network network) {
    Log.i(TAG, "onAvailable called " +
    network.toString());
    if (isLost) {
    isLost = false;}
    // when network available called it
    }


    @Override
    public void onLost(Network network) {
    super.onLost(network);
    isLost = true;
    // when network lost called it
    Log.i(TAG, "onLost called ");
    }
    }





    share|improve this answer


























    • When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

      – Elias Fazel
      Nov 15 '18 at 3:49






    • 1





      please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

      – Sultan Mahmud
      Nov 15 '18 at 4:43











    • As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

      – Elias Fazel
      Nov 15 '18 at 21:56
















    1














    To check network connectivity state change you can do it programmatically. It supports API 21+.



        public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback {
    public final String TAG = ConnectionStateMonitor.class.getSimpleName();
    private boolean isLost = false;


    final NetworkRequest networkRequest;
    Context context;
    private ConnectionStateMonitor instance;
    private boolean isLost = false;

    private ConnectionStateMonitor() {
    networkRequest = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build();
    }

    public synchronized ConnectionStateMonitor getInstance() {
    if (instance == null) {
    instance = new ConnectionStateMonitor();
    }
    return instance;
    }

    public void enable(Context context) {
    try {
    if (context == null) return;
    this.context = context;
    ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.registerNetworkCallback(networkRequest, this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onAvailable(Network network) {
    Log.i(TAG, "onAvailable called " +
    network.toString());
    if (isLost) {
    isLost = false;}
    // when network available called it
    }


    @Override
    public void onLost(Network network) {
    super.onLost(network);
    isLost = true;
    // when network lost called it
    Log.i(TAG, "onLost called ");
    }
    }





    share|improve this answer


























    • When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

      – Elias Fazel
      Nov 15 '18 at 3:49






    • 1





      please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

      – Sultan Mahmud
      Nov 15 '18 at 4:43











    • As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

      – Elias Fazel
      Nov 15 '18 at 21:56














    1












    1








    1







    To check network connectivity state change you can do it programmatically. It supports API 21+.



        public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback {
    public final String TAG = ConnectionStateMonitor.class.getSimpleName();
    private boolean isLost = false;


    final NetworkRequest networkRequest;
    Context context;
    private ConnectionStateMonitor instance;
    private boolean isLost = false;

    private ConnectionStateMonitor() {
    networkRequest = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build();
    }

    public synchronized ConnectionStateMonitor getInstance() {
    if (instance == null) {
    instance = new ConnectionStateMonitor();
    }
    return instance;
    }

    public void enable(Context context) {
    try {
    if (context == null) return;
    this.context = context;
    ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.registerNetworkCallback(networkRequest, this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onAvailable(Network network) {
    Log.i(TAG, "onAvailable called " +
    network.toString());
    if (isLost) {
    isLost = false;}
    // when network available called it
    }


    @Override
    public void onLost(Network network) {
    super.onLost(network);
    isLost = true;
    // when network lost called it
    Log.i(TAG, "onLost called ");
    }
    }





    share|improve this answer















    To check network connectivity state change you can do it programmatically. It supports API 21+.



        public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback {
    public final String TAG = ConnectionStateMonitor.class.getSimpleName();
    private boolean isLost = false;


    final NetworkRequest networkRequest;
    Context context;
    private ConnectionStateMonitor instance;
    private boolean isLost = false;

    private ConnectionStateMonitor() {
    networkRequest = new NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .build();
    }

    public synchronized ConnectionStateMonitor getInstance() {
    if (instance == null) {
    instance = new ConnectionStateMonitor();
    }
    return instance;
    }

    public void enable(Context context) {
    try {
    if (context == null) return;
    this.context = context;
    ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    connectivityManager.registerNetworkCallback(networkRequest, this);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void onAvailable(Network network) {
    Log.i(TAG, "onAvailable called " +
    network.toString());
    if (isLost) {
    isLost = false;}
    // when network available called it
    }


    @Override
    public void onLost(Network network) {
    super.onLost(network);
    isLost = true;
    // when network lost called it
    Log.i(TAG, "onLost called ");
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 4:42

























    answered Nov 15 '18 at 3:32









    Sultan MahmudSultan Mahmud

    23017




    23017













    • When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

      – Elias Fazel
      Nov 15 '18 at 3:49






    • 1





      please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

      – Sultan Mahmud
      Nov 15 '18 at 4:43











    • As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

      – Elias Fazel
      Nov 15 '18 at 21:56



















    • When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

      – Elias Fazel
      Nov 15 '18 at 3:49






    • 1





      please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

      – Sultan Mahmud
      Nov 15 '18 at 4:43











    • As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

      – Elias Fazel
      Nov 15 '18 at 21:56

















    When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

    – Elias Fazel
    Nov 15 '18 at 3:49





    When the network is available it triggers the onAvailable() as soon as calling registerNetworkCallback() so my main problem still remains. I got another solution that I will post asap.

    – Elias Fazel
    Nov 15 '18 at 3:49




    1




    1





    please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

    – Sultan Mahmud
    Nov 15 '18 at 4:43





    please check my latest updated code. It will prevent for the first time call and work well for all uses. Thanks

    – Sultan Mahmud
    Nov 15 '18 at 4:43













    As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

    – Elias Fazel
    Nov 15 '18 at 21:56





    As I posted yesterday the solution of simple variable to add extra restriction on getting the intent action.

    – Elias Fazel
    Nov 15 '18 at 21:56













    0














    [Simple Solution]



    Problem is Sticky Broadcasts which trigger at the moment of registration.
    To avoid this I simply add another parameter to restrict the process.
    I defined a static variable



    public static boolean triggerBroadcast = false;


    Then I change it to after registering the broadcast by few second delay.



    registerReceiver(broadcastReceiverAction, intentFilter);
    new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
    triggerWifiBroadcast = true;
    }
    }, 3000);


    Then onReceive() I compare intent.getAction with this



    if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED") && triggerBroadcast ) {
    //
    }


    Then you can change it to false whenever decide to unregister the broadcast receiver.






    share|improve this answer






























      0














      [Simple Solution]



      Problem is Sticky Broadcasts which trigger at the moment of registration.
      To avoid this I simply add another parameter to restrict the process.
      I defined a static variable



      public static boolean triggerBroadcast = false;


      Then I change it to after registering the broadcast by few second delay.



      registerReceiver(broadcastReceiverAction, intentFilter);
      new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
      triggerWifiBroadcast = true;
      }
      }, 3000);


      Then onReceive() I compare intent.getAction with this



      if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED") && triggerBroadcast ) {
      //
      }


      Then you can change it to false whenever decide to unregister the broadcast receiver.






      share|improve this answer




























        0












        0








        0







        [Simple Solution]



        Problem is Sticky Broadcasts which trigger at the moment of registration.
        To avoid this I simply add another parameter to restrict the process.
        I defined a static variable



        public static boolean triggerBroadcast = false;


        Then I change it to after registering the broadcast by few second delay.



        registerReceiver(broadcastReceiverAction, intentFilter);
        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
        triggerWifiBroadcast = true;
        }
        }, 3000);


        Then onReceive() I compare intent.getAction with this



        if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED") && triggerBroadcast ) {
        //
        }


        Then you can change it to false whenever decide to unregister the broadcast receiver.






        share|improve this answer















        [Simple Solution]



        Problem is Sticky Broadcasts which trigger at the moment of registration.
        To avoid this I simply add another parameter to restrict the process.
        I defined a static variable



        public static boolean triggerBroadcast = false;


        Then I change it to after registering the broadcast by few second delay.



        registerReceiver(broadcastReceiverAction, intentFilter);
        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
        triggerWifiBroadcast = true;
        }
        }, 3000);


        Then onReceive() I compare intent.getAction with this



        if (intent.getAction().equals("android.net.wifi.WIFI_STATE_CHANGED") && triggerBroadcast ) {
        //
        }


        Then you can change it to false whenever decide to unregister the broadcast receiver.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 21:54

























        answered Nov 15 '18 at 4:00









        Elias FazelElias Fazel

        1,07879




        1,07879






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311657%2fi-am-receiving-android-net-wifi-wifi-state-changed-as-soon-as-registering-broa%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine