Skip to content

Instantly share code, notes, and snippets.

@trinadhkoya
Forked from alphamu/NetworkHelper.java
Created April 25, 2017 15:08
Show Gist options
  • Save trinadhkoya/a791bd64413f1e24a9eeda3f98ad5bad to your computer and use it in GitHub Desktop.
Save trinadhkoya/a791bd64413f1e24a9eeda3f98ad5bad to your computer and use it in GitHub Desktop.
Gist showing the use case of a headless Fragment to check if internet is available
public class NetworkHelper extends Fragment {
public static final String TAG = "NetworkHelper";
public static final String CHECK_INTERNET = "network_connection";
private Activity mActivity;
AlertDialog mAlertDialog = null;
private BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(CHECK_INTERNET) && !intent.getBooleanExtra(CHECK_INTERNET, true)) {
showAlertDialog(mActivity, "Internet Connection",
"No internet connection available.\n\n" +
"Please check your internet connection and try again.");
} else {
if (mAlertDialog != null && mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
mAlertDialog = null;
}
}
}
};
public static NetworkHelper newInstance() {
return new NetworkHelper();
}
public NetworkHelper() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
@Override
public void onResume() {
super.onResume();
IntentFilter iff = new IntentFilter(CHECK_INTERNET);
LocalBroadcastManager.getInstance(mActivity).registerReceiver(onNotice, iff);
if (!isInternetConnected(mActivity)) {
showAlertDialog(mActivity, "Internet Connection",
"No internet connection available.\n\n" +
"Please check your internet connection and try again.");
}
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(mActivity).unregisterReceiver(onNotice);
}
@Override
public void onDetach() {
super.onDetach();
mActivity = null;
}
public void showAlertDialog(Context context, String title, String message) {
if (mAlertDialog != null && mAlertDialog.isShowing()) {
return; //already showing
} else if (mAlertDialog != null) {
mAlertDialog.dismiss();
mAlertDialog = null;
}
mAlertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
mAlertDialog.setTitle(title);
// Setting Dialog Message
mAlertDialog.setMessage(message);
// Setting OK Button
mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
getString(android.R.string.ok),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mAlertDialog = null;
}
});
// Showing Alert Message
mAlertDialog.show();
}
public static boolean isInternetConnected(Context context) {
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
// Here if condition check for wifi and mobile network is available or not.
// If anyone of them is available or connected then it will return true,
// otherwise false;
if (wifi != null && wifi.isConnected()) {
return true;
} else if (mobile != null && mobile.isConnected()) {
return true;
}
return false;
}
public static boolean isWifiConnected(Context context) {
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi != null && wifi.isConnected()) {
return true;
}
return false;
}
public static boolean isMobileDataConnected(Context context) {
ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobile != null && mobile.isConnected()) {
return true;
}
return false;
}
}
public class NetworkChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent in = new Intent(NetworkHelper.CHECK_INTERNET);
in.putExtra(NetworkHelper.CHECK_INTERNET,
NetworkHelper.isInternetConnected(context));
LocalBroadcastManager.getInstance(context).sendBroadcast(in);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment