Created
January 8, 2018 05:20
-
-
Save iniyanmurugavel/e03e98955a41b4847d1ed5e9252ed5cf to your computer and use it in GitHub Desktop.
Network checking call in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <uses-permission android:name="android.permission.INTERNET" /> | |
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
| ConnectivityReceiver.java | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.net.ConnectivityManager; | |
| import android.net.NetworkInfo; | |
| public class ConnectivityReceiver | |
| extends BroadcastReceiver { | |
| public static ConnectivityReceiverListener connectivityReceiverListener; | |
| public ConnectivityReceiver() { | |
| super(); | |
| } | |
| @Override | |
| public void onReceive(Context context, Intent arg1) { | |
| ConnectivityManager cm = (ConnectivityManager) context | |
| .getSystemService(Context.CONNECTIVITY_SERVICE); | |
| NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
| boolean isConnected = activeNetwork != null | |
| && activeNetwork.isConnectedOrConnecting(); | |
| if (connectivityReceiverListener != null) { | |
| connectivityReceiverListener.onNetworkConnectionChanged(isConnected); | |
| } | |
| } | |
| public static boolean isConnected() { | |
| ConnectivityManager | |
| cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext() | |
| .getSystemService(Context.CONNECTIVITY_SERVICE); | |
| NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); | |
| return activeNetwork != null | |
| && activeNetwork.isConnectedOrConnecting(); | |
| } | |
| public interface ConnectivityReceiverListener { | |
| void onNetworkConnectionChanged(boolean isConnected); | |
| } | |
| } | |
| MyApplication.java | |
| import android.app.Application; | |
| public class MyApplication extends Application { | |
| private static MyApplication mInstance; | |
| @Override | |
| public void onCreate() { | |
| super.onCreate(); | |
| mInstance = this; | |
| } | |
| public static synchronized MyApplication getInstance() { | |
| return mInstance; | |
| } | |
| public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) { | |
| ConnectivityReceiver.connectivityReceiverListener = listener; | |
| } | |
| } | |
| Activity | |
| main inside | |
| checkConnection(); | |
| // Method to manually check connection status | |
| private void checkConnection() { | |
| boolean isConnected = ConnectivityReceiver.isConnected(); | |
| condition(isConnected); | |
| } | |
| implements ConnectivityReceiver.ConnectivityReceiverListener { | |
| public void condition(){ | |
| if (isConnected) { | |
| message = "Good! Connected to Internet"; | |
| } else { | |
| message = "Sorry! Not connected to internet"; | |
| } | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| // register connection status listener | |
| MyApplication.getInstance().setConnectivityListener(this); | |
| } | |
| /** | |
| * Callback will be triggered when there is change in | |
| * network connection | |
| */ | |
| @Override | |
| public void onNetworkConnectionChanged(boolean isConnected) { | |
| showSnack(isConnected); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment