Skip to content

Instantly share code, notes, and snippets.

@aniXification
Created June 30, 2013 18:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save aniXification/5896231 to your computer and use it in GitHub Desktop.
Save aniXification/5896231 to your computer and use it in GitHub Desktop.
Check internet connection change via Broadcast Receiver in background. #android
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;
}
}
@sasisereen89
Copy link

so how can i register this broadcast in my activity?

@ShreyashPromact
Copy link

@sasisereen89

You can register your BroadcastReceiver within your AndroidManifest.xml file or temporarily using the Context object. For listening to connectivity changes I recommend to register your receiver programmatically. Since Activities inherit from Context you can simple call the registerReceiver(BroadcastReceiver, IntentFilter) method:

Please check below example for same:

registerReceiver(
new ConnectivityChangeReceiver(),
new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));

@eoinahern
Copy link

getAllNetworkInfo(); This method was deprecated in API level 23.

@SMontiel
Copy link

@Awais00094
Copy link

not working on android 7.0

@Crysis21
Copy link

Crysis21 commented Mar 2, 2018

Registering your receiver in manifest has been deprecated since API 24. It only works now if you use Context.registerReceiver(). Another solution I can think off is to use the AlarmManager to do scheduled checks. If you app depends on the connectivity change during it's lifecycle, than it won't be affected by using context registration.

@dilahow
Copy link

dilahow commented May 9, 2018

ShreyashPromact
thanks

@tintinscorpion
Copy link

This will only detect that the device is connected to a network but what if the network doesnot provide Internet connection?
So, basically I will suggest that with use of networking classes call the server api of your project which is working and if there is response then Connection established else not established.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment