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