With this method i know when the user is connected or not.
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
in my application when the user press send it will upload some data to the server, if there is not conectivity it will say “we will try to send it later” but how i can check the internet status and only when the connectivity is on, send the information?
First, use an
IntentServiceto handle all of your data uploads. Have it check for connectivity with the method you have – if connected, upload the data otherwise store it (useSharedPreferencesor a database).Second create a
BroadcastReceiverand register it in the manifest with the following<intent-filter>…NOTE: You will need the following permissions…
In the
onReceive(...)method of theBroadcastReceiverhave it check to see if any data is pending and, if there is, have it start theIntentServiceto send the data.Also, use
Notificationscreated by theIntentServiceto give feedback to the user.