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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:04:33+00:00 2026-05-22T21:04:33+00:00

I have an app in android which is a sort of a client side

  • 0

I have an app in android which is a sort of a client side of a TCP/IP connection …this app has the use of receiving GPSt data from a GPS provider and sending it to the server side of my TCP/IP connection.

Only that when there is no internet connection the GPS data I have to store it in a DB….and as soon as I have again internet connection I have to start again the client side and reconnect to my server and send the data.

Question:1.How could I detect internet connection in android?(my app is running on emulator)

2.Is possible,as soon as I detect internet connection to ask to my thread client to reconnect to the server???

Here is a scheme of my code:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.client);

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationListener = new MyLocationListener();



    cThread = new Thread(new ClientThread(syncToken));

    cThread.start();

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locationListener);
}

public void onLocationChanged(Location loc) {
        if (loc != null) {

                    latitude = (int) (loc.getLatitude() * 1E6);

                    longitude = (int) (loc.getLongitude() * 1E6);


                    }
                }



            GeoPoint p = new GeoPoint(latitude, longitude);
            // geoPointsArray.add(p);
            db.insertData1(longitude, latitude);




        }

public class ClientThread implements Runnable {
    Object syncToken;



    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);

            socket = new Socket(serverAddr, 7001);

            Log.d(" ", "Clientul s-a conect");

        } catch (UnknownHostException e) {

            System.err.println("Don't know about host");

        } catch (IOException e) {

            System.err
                    .println("Couldn't get I/O for the connection to host");
        }

        try {

            os = new ObjectOutputStream(socket.getOutputStream());

        } catch (IOException e) {


            System.out.println(e);
        }

        while (true) {


            synchronized (syncToken) {

                try {
                    syncToken.wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }
            }

            if (socket != null && os != null) {

                    try {

            //send data through the socket

                    }catch (Exception e){

                        e.printStackTrace();

                    }

                    i++;

                }

            }


    }
}

EDIT:

Here is what I did:

private NetworkStateReceiver mNetSateReceiver = null;

private class NetworkStateReceiver extends BroadcastReceiver

{
    @Override

    public void onReceive( Context context, Intent intent )

    {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info =connectivity.getActiveNetworkInfo();;

        if (info != null && info.isConnectedOrConnecting()) {


            System.out.println(" internet connection!");


        } 
        else
            System.out.println("no internet connection");



    }
}

onCreate(){



     registerReceiver( mNetSateReceiver, new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION ) );

        syncToken = new Object();

        cThread = new Thread(new ClientThread(syncToken));

        cThread.start();


}

 public void onDestroy(){ 

     super.onDestroy(); 

     db.close();

        unregisterReceiver( mNetSateReceiver );
 }

I understand that everytime my state connection changes my onReceive() gets called….that means that I should start my thread in onReceive() when there is internet conection????

I’m a little bit confuse if u could clear up a little bit the thing for me…and where should I start that Intent that u are telling me??
Thx

  • 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-22T21:04:34+00:00Added an answer on May 22, 2026 at 9:04 pm

    This thread answers your question

    Regarding something happening when its up. You could then raise an broadcast intent and receive the intent by an activity which will carry out the update once internet is available

    I hope that helps

    Edit: by broadcasting an intent i meant something like this. I’m not sure if this is the best solution someone else might provide input…

    The following intent broadcast can run if internet is detected.

    code to broadcast intent:

      Intent i = new Intent(DO_REFRESH);
      sendBroadcast(i);
    

    broad to receive intent:

    doRefreshBroadcastReceiver drbr;
    ...
    
    onResume() {
      // register the refreshing complete broadcast receiver
      IntentFilter intentFilter = new IntentFilter(DO_REFRESH);
      drbr = new doRefreshBroadcastReceiver();
      registerReceiver(drbr,intentFilter);        
    }
    
        public class doRefreshBroadcastReceiver extends BroadcastReceiver { 
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                // call method to run fetch code...
            }
        } 
    
    • 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 I want to use instead of the standard android
I have a class which extends android.app.Application , which I use to persist global
I have an app in android which is a kind of client-server. I've established
I have an Android app which I use to register users on my web
I have an Android App which contains an external dependend jar library. This external
I have an app in android in which I return some data from facebook
I have an android app which uses camera. I am using a socket connection
I have an android app which connects to a server using a socket connection
I have a simple client-server app on android. the android service communicates with the
I have an Android App which consists on different modules. The Main module is

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.