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 6004027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:12:18+00:00 2026-05-23T01:12:18+00:00

I have an app in which I’m trying to detect WHEN the Internet connection

  • 0

I have an app in which I’m trying to detect WHEN the Internet connection appears and when it disappears.

At the moment, when it appears, I’m starting a new thread (different from the UI) which connects my app to a remote server.

For that I’m hardly trying to implement a broadcast receiver which LISTENS for connectivity, but I’m having problems in understanding the concept.

In my onCreate() I have somethig like:

onCreate()    
{
     cThread = new Thread(new ClientThread(syncToken));
     cThread.start();
}

When there is connection to the Internet I’m sending data through the socket, when there is not I’m storing the data in a database. And when the Internet appears I’m restarting my thread to reconnect and send the old data (which hasn’t been sent because of network crashing) and the new one.

Let’s say I would implement something like this:

DoRefreshBroadcastReceiver refreshBroadcastReceiver; 
...    
onResume() {    
  // register the refreshing complete broadcast receiver    
  IntentFilter intentFilter = new IntentFilter(DO_REFRESH);    
  refreshBroadcastReceiver = new doRefreshBroadcastReceiver();    
  registerReceiver(refreshBroadcastReceiver, intentFilter);        
}

public class DoRefreshBroadcastReceiver extends BroadcastReceiver {     
    @Override
    public void onReceive(Context context, Intent intent) {    
        // call method to run fetch code...
    }
} 

Does this mean that when the Internet connection is detected my onReceive() gets called? And I could start my thread there?

What is the concept of using an intent? Because I really don’t get it. How to use it, and what its purpose?


THE IDEA: I don’t really know how to use this intent in this case or how to use it in my app!

Would this thing detect the connection to the Internet even when I’m not in this activity?


EDIT:

Here is how my onReceive looks like:


onCreate()
{
    cThread = new Thread(new ClientThread(syncToken));
    // cThread.start();

    connIntentFilter = new IntentFilter(
                             "android.net.conn.CONNECTIVITY_CHANGE");
    connListener = new MyConnectivityListener();
}

public void onReceive(Context context, Intent intent)
{
    mNetworkInfo = (NetworkInfo) intent
            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

    if (mNetworkInfo != null && mNetworkInfo.isConnected())
    {
        /*
         * if(mNetworkInfo.getType()==ConnectivityManager.TYPE_WIFI);
         *
         *
         * else
         */

        cThread.start();
    }

    else {
        System.out.println("There is no internet connection!");

        try {
            cThread.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

mNetworkInfo != null && mNetworkInfo.isConnected()    

Does this mean it’s connected or should I verify for a certain type of connection on the emulator?

*I think that I should start my thread directly in onReceive(). As soon as my app starts it detects the Internet connection and BroadcastReceiver gets fired, doesn’t it?

  • 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-23T01:12:18+00:00Added an answer on May 23, 2026 at 1:12 am

    Try something like this…

    public class MyActivity extends Activity {
    
        private MyConnectivityListener connListener = null;
        private IntentFiler connIntentFilter = null;
        private Boolean connIntentFilterIsRegistered = false;
    
        @Override
        protected void onCreate(...) {
            ...
            connIntentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
            connListener = new MyConnectivityListener();
        }
    
        @Override
        protected void onResume() {
            ...
            if (!connIntentFilterIsRegistered) {
                registerReceiver(connListener, connIntentFilter);
                connIntentFilterIsRegistered = true;
            }
        }
    
        @Override
        protected void onPause() {
            ...
            if (connIntentFilterIsRegistered) {
                unregisterReceiver(connListener);
                connIntentFilterIsRegistered = false;
            }
        }
    
        protected class MyConnectivityListener extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                // The NetworkInfo for the affected network is sent
                // as an extra; it should be consulted to see what
                // kind of connectivity event occurred.
            }
        }
    }
    

    A BroadcastReceiver is effectively a ‘listener’ which listens for events either sent by the system or, in some cases, by your own application components.

    In this case, the system broadcasts android.net.conn.CONNECTIVITY_CHANGE whenever there is a connection change (connected/disconnected). By registering your BroadcastReceiver to ‘listen’ for that event, you can get the extra included in the Intent from your BroadcastReceiver’s onReceive(...) method and do whatever you need to do accordingly. The extra is a `NetworkInfo object which will contain information about the particular network and whether it is connected or not.

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

Sidebar

Related Questions

I have an app which consists of several different assemblies, one of which holds
I have a web app which connects to a server using a TCP connection
I have an app which I'm trying to restrict permissions to while I'm doing
I have an app which could benefit from the user being able to choose
I have wxPython app which is running on MS Windows and I'd like it
So I have an app which plays many short sound clips. I need to
I have a small app which references the Microsoft.SqlServer.Smo assembly (so I can display
I have an MFC app which I have been working on for a few
I have a WPF app which snaps to screen edges (I just set the
I have a C# app which uses a System.Diagnostics.Process to run another exe. I

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.