Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4592552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:33:19+00:00 2026-05-21T22:33:19+00:00

In my app I have a BroadcastReceiver that is launched as a component through

  • 0

In my app I have a BroadcastReceiver that is launched as a component through a <receiver> tag, filtering android.net.conn.CONNECTIVITY_CHANGE intents.

My goal is simply to know when a Wifi connection was established, so what I am doing in onReceive() is this:

NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected()) {
    // Wifi is connected
}

It works fine, but I always seem to get two identical intents within about one second when a Wifi connection is established. I tried to look at any info I could get from the intent, the ConnectivityManager and WifiManager, but I can’t find anything that distinguishes the two intents.

Looking at the log, there is at least one other BroadcastReceiver that also receives the two identical intents.

It is running on a HTC Desire with Android 2.2

Any idea why I seem to get a “duplicated” intent when Wifi connects or what the difference between the two might be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:33:20+00:00Added an answer on May 21, 2026 at 10:33 pm

    NOTE: For a recent, up-to-date answer, see this one below!

    After a lot of googling and debugging, I believe this is the correct way to determine if Wifi has connected or disconnected.

    The onReceive() method in the BroadcastReceiver:

    public void onReceive(final Context context, final Intent intent) {
    
    if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
        NetworkInfo networkInfo =
            intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if(networkInfo.isConnected()) {
            // Wifi is connected
            Log.d("Inetify", "Wifi is connected: " + String.valueOf(networkInfo));
        }
    } else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        NetworkInfo networkInfo =
            intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
            ! networkInfo.isConnected()) {
            // Wifi is disconnected
            Log.d("Inetify", "Wifi is disconnected: " + String.valueOf(networkInfo));
        }
    }
    }
    

    Together with the following receiver element in AndroidManifest.xml

    <receiver android:name="ConnectivityActionReceiver"
        android:enabled="true" android:label="ConnectivityActionReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            <action android:name="android.net.wifi.STATE_CHANGE"/>
        </intent-filter>
    </receiver>
    

    Some explanation:

    • When only considering ConnectivityManager.CONNECTIVITY_ACTION, I always get two intents containing identical NetworkInfo instances (both getType() == TYPE_WIFI and isConnected() == true) when Wifi connects – the issue described in this question.

    • When only using WifiManager.NETWORK_STATE_CHANGED_ACTION, there is no intent broadcasted when Wifi disconnects, but two intents containing different NetworkInfo instances, allowing to determine one event when Wifi is connected.

    NOTE: I’ve received one single crash report (NPE) where the intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO) returned null. So, even if it seems to be extremely rare to happen, it might be a good idea to add a null check.

    Cheers,
    Torsten

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an BroadcastReceiver for handling new or modified applications: <receiver android:name=.PackageHandler > <intent-filter>
I have an app that receives push notifications from C2DM server with a BroadcastReceiver.
Suppose that, I have an android app that launches browser with some url supplied
I have an Android app that uses the AlarmService. According to the docs, I
have an Android app that uses the RECEIVE_BOOT_COMPLETED. According to the docs, I should
In my app I have a broadcast receiver that turns on GPS upon receiving
I have an app that send sms to other android or non-android phones. You
My app uses a BroadcastReceiver and a service to perform background updates. I have
I have my app that have some tab's (tabbar). In the first tab, there
Ok so I have two BroadcastReceiver registered. When the app is closed they both

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.