Skip to content

Instantly share code, notes, and snippets.

@tony1223
Last active December 18, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tony1223/5732191 to your computer and use it in GitHub Desktop.
Save tony1223/5732191 to your computer and use it in GitHub Desktop.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class MyNetworkConnectedReceiver extends BroadcastReceiver {
private Context _context;
@Override
public void onReceive(Context context, Intent intent) {
_context = context;
ConnectivityManager connectivityManager = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE));
NetworkInfo currentNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (currentNetworkInfo != null && currentNetworkInfo.isConnected()) {
//do something when network connected.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment