Detecting internet connection in android

I have an Android application that is a kind of client side of a TCP / IP connection ... this application uses the GPS data from the GPS provider and sends it to the server side of my TCP / IP.

Only when there is no Internet connection, GPS data that I have to store in the database .... and as soon as I reconnect to the Internet, I need to start the client side again and reconnect to my server and send the data.

Question: 1.How can I determine my internet connection in Android? (my application runs on an emulator)

2. Perhaps as soon as I discover an Internet connection to ask my thread client to connect to the server?

Here is the diagram 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 sa 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 every time my state connection changes my onReceive (), it is called ... does this mean that I have to start my flow in onReceive () when there is an Internet connection ????

I'm a little embarrassed if you could sort this out a bit for me ... and where should I start this intention that you tell me ??? thanks

+4
source share
1 answer

This thread answers your question.

Regarding what happens when it happens. Then you can increase the intent of the broadcast and get the intention through the activity that will perform the update after accessing the Internet

I hope this helps

Edit: by translating the intention, I had in mind something similar. I'm not sure if this is the best solution for someone else ...

The next broadcast of intentions may be performed if the Internet is detected.

broadcast code:

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

wide for receiving 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
source

All Articles