I am developing an app that has following requirements 1)It should check the internet connectivity continuously in the background 2)when connectivity is found,it should send the email automatically 3) if the connectivity is not found,it should wait and keep checking the connectivity after every 10 or 15 sec. i have searched through and through but cannot find the connectivity check issue anywhere. I am new in android development. Please help me resolve this issue. Thank You.
Asked
Active
Viewed 2,478 times
0
-
4Why Javascript tag? How it is related to android development? – Pradeep Simha Dec 17 '15 at 06:45
-
1) Use a timer 2) Ping/send messages to your server i.e the one you want to reach during that time 3) Typically you want to use backoff mechanism i.e the waiting time increases with each failure to reach the server since it would cause a load on your server, the way gmail implements wait mechanism. – Madusudanan Dec 17 '15 at 06:47
-
add background service . – Abhinav singh Dec 17 '15 at 06:54
-
`> 500` such questions already available in SO. Search before asked – M D Dec 17 '15 at 06:56
-
would you suggest the isSync function? and how does it work for the same purpose? – AB1 Dec 17 '15 at 06:56
-
please be generous enough to provide the links for the same if 500 such questions are already available!! – AB1 Dec 17 '15 at 06:57
-
and please before asked questions in SO. take a look [How to ask?](http://stackoverflow.com/help/how-to-ask) – M D Dec 17 '15 at 07:03
-
@AB1: Avanti B. what you have tried for? – ρяσѕρєя K Dec 17 '15 at 07:12
4 Answers
0
Set alarm manager for your app which checks internet connection after every 15 seconds. Alarm manger broadcast an intent where you can do your stuff.

Chandrakant Dvivedi
- 653
- 4
- 13
-
If you could please elaborate the use of alarm manager, it would be really helpful!! – AB1 Dec 17 '15 at 06:51
-
Read this documentation for how to set Alarm manager in your app http://developer.android.com/training/scheduling/alarms.html – Chandrakant Dvivedi Dec 17 '15 at 06:57
0
you can use a broadcast receiver that will check for the internet connectivity and whenever any change in the network it will be called.
public class ConnectivityReceiver extends BroadcastReceiver {
private static boolean firstConnect = true;
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null) {
boolean isConnected = activeNetInfo.isConnected();
boolean isWiFi = activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI;
boolean isMobile = activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE;
if (firstConnect) {
firstConnect = false;
//checks for the type of network, device is connected to.
if (isConnected && isWiFi) {
if(!Utilities.isReleaseMode(context)){
Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
}
Log.i("Wifi Network connected", "wifi Network connected");
} else if (isConnected && isMobile) {
if(!Utilities.isReleaseMode(context)) {
Toast.makeText(context, "Connected to Internet", Toast.LENGTH_SHORT).show();
}
Log.i("Mobile Network ", "mobile Network connected");
}
}
} else {
firstConnect = true;
// Log.i("Network Disconnected", "Network Disconnected");
}
}

Mohit
- 505
- 8
- 31
-
A change in the network might not be related to internet connectivity restoration, it is always better to reach a real server to check connectivity. – Madusudanan Dec 17 '15 at 06:50
-
Performance wise i would prefer to have a broadcast receiver, rather then calling real server periodically. – Mohit Dec 17 '15 at 06:52
-
I would prefer a backoff mechanism as I mentioned, where the wait time is increased with each failure attempt to ping, that is the way almost many apps are implemented – Madusudanan Dec 17 '15 at 06:56
0
Check internet connectivity:
public static boolean isNetworkStatusAvialable (Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null)
{
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if(netInfos != null)
if(netInfos.isConnected())
if (netInfos.isAvailable())
return true;
}
return false;
}
if (isNetworkStatusAvialable(getApplicationContext()))
{
// your function
}else{}

MurugananthamS
- 2,395
- 4
- 20
- 49
-
but this will just check the internet connectivity. i want to add further details to it that it should continuously keep checking for internet. and if the connection is found, it should fetch my file from the internal storage and send it automatically. – AB1 Dec 17 '15 at 06:55
-
call this function into Asynchronous task ,it will run backround Automatically @AvantiBhandarkar – MurugananthamS Dec 17 '15 at 06:59
0
Call this function every 10 or 15 sec using alaram.
public static boolean isOnline(Context context) {// requires network state
// access permisstion
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
try {
isOnline(getActivity());
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 10000);

RDY
- 613
- 1
- 9
- 25