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

  • Home
  • SEARCH
  • 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 6388227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:15:10+00:00 2026-05-25T03:15:10+00:00

One time that I had a prob, you guys were really helpful. So here

  • 0

One time that I had a prob, you guys were really helpful. So here I am again with another problem I have fallen onto… :/

I am running a custom NetworkReceiver that extends BroadcastReceiver. I want to detect when the phone has an internet connection so that I can start a service whenever it does. I have read a lot of topics and all topics more or less prompt to the following code which DOES NOT WORK for me:

public class NetworkReceiver extends BroadcastReceiver {

private static final String tag = NetworkReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(tag,"****** NetworkReceiver // onReceive()");

    boolean noConnection = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
        Log.i(tag,"****** NetworkReceiver // Network Down?: " + intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false));
    }

    // TODO Bug: Never stops the service. Stays always connected!
    if(noConnection) {
        Log.i(tag,"****** NetworkReceiver // Stopping Service...");
        context.stopService(new Intent(context, FetchService.class));
    } else {
        Log.i(tag,"****** NetworkReceiver // Starting Service!");
        context.startService(new Intent(context, FetchService.class));
    }
}

The problem I have is that the logcat always returns NetworkDown?: false and the service never stops (it restarts all the time). I tried it on the emulator by pressing F8 and I also tried it on my dev-phone (it has no SIM, internet access is by my wifi router) by switching off the router -> no internet access guaranteed!

My Manifest includes the following lines:

...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<receiver android:name=".NetworkReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
...

I should also note that I also tried
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> in my manifest file with no change and that when I altered the line:
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)
to
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true)
the result was always true – and thus it always tried to stop my service and never start it.

I suspect that the Extras from the Intent are not read at all and that is why the default value of getBooleanExtra() always returns.

PS. I am on Android 2.1

Thanks!

  • 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-25T03:15:11+00:00Added an answer on May 25, 2026 at 3:15 am

    I am not sure for the Extras that cannot be read inside the BroadcastReceiver, but I can show you athe way I use the BroadcastReceiver for a purpose similar to yours.

    My receiver does only catch the connectivity changes, but it does NOT evaluate the connectivity status. It informs the Service instead as in both cases (connected or not) this service will have something to do. This service can be running or not at that time, so it will either be started or “informed” (remember that you can safely call startService() several times, these calls do not nest):

    public class ConnectivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final Intent intentService = new Intent(context, MyService.class);
            intentService.putExtra(MyService.INTENT_HANDLE_CONNECTIVITY_CHANGE, "");
    
            context.startService(intentService);
        }
    }
    

    Then, in my service, I do evaluate the connectivity and then act accordingly. Notice that I don’t evaluate the connection status the same way as you do, and this might be why the code below works but not yours (below is the relevant code only):

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            final Bundle bundle = intent.getExtras();
    
            // Handle the intent INTENT_HANDLE_CONNECTIVITY_CHANGEif any
            if ((bundle != null) && (bundle.get(INTENT_HANDLE_CONNECTIVITY_CHANGE) != null)) {         
                handleConnectivity(); 
            }
        }
    
        return START_STICKY;
    }
    
    private void handleConnectivity() {
        final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo netInfo = cm.getActiveNetworkInfo();
    
        if(netInfo != null && netInfo.isConnected()) {
            // WE ARE CONNECTED: DO SOMETHING
        }   
        else {
            // WE ARE NOT: DO SOMETHING ELSE    
        }
    }
    

    This works fine, under Android-7 (2.1) as well.

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

Sidebar

Related Questions

I am trying to process files one at a time that are stored over
I distinctly remember that, at one time, the guideline pushed by Microsoft was to
I need an example algorithm that will draw pixels one at a time on
One mentor I respect suggests that a simple bean is a waste of time
I have created a one-time subscription in SSRS report manager 2008. However I keep
I have been working with .Net since VS2002 and since then any time that
I was trying to program a Timer class (unaware that boost had one), then
Good day everyone. This problem was part of another one which it as been
I understand SSO as the one time login for all the applications connected. I
Unfortunately, despite having tried to learn regex at least one time a year for

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.