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);
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 {
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