1

Can anyone help me how to show snackbar when WiFi or Mobile data not available on button press. Actually this button using for to send an email when user wants to send feedback form from my Android app.

My Requirement is: When user press button If any network available, then E-mail should send directly without showing Snackbar and snackbar should show when any network unavailable. I have been trying to solve my issue for last few days but no luck. My snackbar coding works all time like it showing everytime with and without network on button press on below API level 23 and not above.

Note: My code is working perfectly if don't give Snackbar coding. So I don't need email intent coding just I need network status coding with Snackbar.

If anyone provides correct solution it will help me a lot. Thank you in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ashok
  • 55
  • 1
  • 10

2 Answers2

3

Use below function to check if internet is available or not

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

If it returns true, continue sending the email. Else display the snackbar.

You will need to add below permission to your manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Nikhil Gupta
  • 881
  • 6
  • 14
  • Thank you for your code. I'll edit the code and let you know response. – Ashok Apr 16 '18 at 09:41
  • Your code is SIMPLE and TRICKY 1++. Working perfect as I want. Thank you so much for your awesome help.I need another help from you, please check the link given below: https://stackoverflow.com/questions/49482772/how-to-open-common-incomplete-products-link-with-different-toolbar-in-android-st Hope I get a better solution for this also. Once again Thank you Mr. Nikhil Gupta. – Ashok Apr 17 '18 at 00:34
2

ConnectionDetect.class

 import android.content.Context;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;


public class ConnectionDetect
{
private Context context;
public static boolean chechkagain;

public ConnectionDetect(Context context) {
    this.context = context;
}

public boolean isConnectingToInternet() {

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = cm
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo datac = cm
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if ((wifi != null & datac != null)
            && (wifi.isConnected() | datac.isConnected())) {
        chechkagain = true;
        synchronized (context) {
            context.notify();
        }
    } else {
         chechkagain = false;
     }


    return chechkagain;
}
}

Now check internet available or not

   if (connectionDetect.isConnectingToInternet()) {
        //Send Email
    } else {

     Snackbar snackbar = Snackbar
                .make(coordinatorLayout, 
    "Please check internet", 
    Snackbar.LENGTH_LONG);
        snackbar.show();

    }

coordinatorLayout is id of Coordinator Layout inside which we want to show snack bar

Janku
  • 238
  • 2
  • 10
  • Thank you for your code. I'll edit the code and let you know response. – Ashok Apr 16 '18 at 09:40
  • I'm sorry to say, I tried your code but it's not working. Showing error message "if (connectionDetect.isConnectingToInternet()) { at isConnectingToInternet()". Anyway thank you for your help. If you can help me on another problem please check it at: https://stackoverflow.com/questions/49482772/how-to-open-common-incomplete-products-link-with-different-toolbar-in-android-st Hope, I get a solution. Thank you.. – Ashok Apr 17 '18 at 01:03
  • it is showing error just because you have to make a object of ConnectionDetect Class then it will work.. ConnectionDetect connectionDetect = new ConnectionDetect(this); .. before if statement – Janku May 08 '18 at 05:52