Reputation: 1335
I am working on android project which communicate with a webservice in order to receive datas and store them into the database.
Before the sending operation, I verify the connection state (see the code below)
if (XXX.getInstance(Context).isOnline(Context))
{
//TODO Sending operation
}
But during this operation if the device is offline I lose datas though the HTTP status code is 200 or 201.
I manage before the operation but not during.
Is there a way to figure this out?
Upvotes: 0
Views: 217
Reputation:
Use a broadcast receiver to constantly check the status of the connection.
First register the receiver in your onCreate()
method:
registerReceiver(mReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
Then write what you want to do when your desired signal has been discovered:
private BroadcastReceiver mReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
//You are still connected
}
else
{
//You have lost connection
}
}
};
Make sure you unregister the receiver once you leave the activity (in the onDestroy()
) method:
if (mReceiver != null)
{
unregisterReceiver(mReceiver);
}
Upvotes: 1