Skip to content

Instantly share code, notes, and snippets.

@rakshith-sangamone
Forked from aniXification/NetworkChangeReceiver
Last active August 29, 2015 14:15
Show Gist options
  • Save rakshith-sangamone/ff5d8ae54dfe31f6b0b3 to your computer and use it in GitHub Desktop.
Save rakshith-sangamone/ff5d8ae54dfe31f6b0b3 to your computer and use it in GitHub Desktop.
public class NetworkChangeReceiver extends BroadcastReceiver{
private static final String LOG_TAG = "NetworkChangeReceiver";
private boolean isConnected = false;
@Override
public void onReceive(Context context, Intent intent) {
Log.v(LOG_TAG, "Receieved notification about network status");
isNetworkAvailable(context);
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
if (!isConnected) {
Log.v(LOG_TAG, "Now you are connected to Internet!");
Toast.makeText(context, "Internet availablle via Broadcast receiver", Toast.LENGTH_SHORT).show();
isConnected = true;
// do your processing here ---
// if you need to post any data to the server or get
// status
// update from the server
}
return true;
}
}
}
}
Log.v(LOG_TAG, "You are not connected to Internet!");
Toast.makeText(context, "Internet NOT availablle via Broadcast receiver", Toast.LENGTH_SHORT).show();
isConnected = false;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment