1

I want to check for internet connection and if it's available call getData() but if it's not available show a dialog with RETRY and CANCEL options.

If RETRY is clicked check for internet connection; if available call getData but if it isn't available, show the dialog again (something like looping).

Alternatively, if CANCEL is clicked exit the app altogether.

I'm using this class to check for network availability and internet connection:

public class NetworkCheck {

    public static boolean isAvailableAndConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null;
        boolean isNetWorkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected();

        return isNetWorkConnected;

    }

}

And in MainActivity I do this:

if (NetworkCheck.isAvailableAndConnected(this)) {
            //Caling method to get data
            getData();
        } else {
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("No Internet Connection");
            alertDialogBuilder.setMessage("Failed to load. Please ensure ypu're connected to the internet and try again");
            alertDialogBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (!NetworkCheck.isAvailableAndConnected(context)) {
                        alertDialogBuilder.show();
                    } else {
                        getData();
                    }


                }
            });
            alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });
            alertDialogBuilder.show();

        }

From the above codes, I have three questions:

  1. In line if (!NetworkCheck.isAvailableAndConnected(context)) { context is being highlighted in red and when I hover it, I see Cannot resolve symbol 'context'. If I leave the method empty or type this or getActivity; Android Studio complains. Which parameter show I pass the method?
  2. Calling finish() will only kill the activity. Shoudn't the whole app be killed and how?
  3. What else am I doing wrong?
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Faraday
  • 327
  • 1
  • 6
  • 19

1 Answers1

1

1) If you're not using fragments, on the place of context you could do a workaround like this (first you declare):

        final Context mContext;
        mContext = this;

Then you substitute context to mContext

2) Killing the activity is not a recommendation (you probably already read about Activity Lifecycle), but you could try android.os.Process.killProcess(android.os.Process.myPid());

statosdotcom
  • 3,109
  • 2
  • 17
  • 40
  • Where in MainActivity should I declare `final Context mContext; mContext = this;` please? – Faraday Apr 03 '16 at 01:11
  • } else { final Context mContext; mContext = this; final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); – statosdotcom Apr 03 '16 at 01:18
  • I did that and also this: `if (!NetworkCheck.isAvailableAndConnected(mContext())) {` `mContext` was underlined in red with the info **method call expected** – Faraday Apr 03 '16 at 01:19
  • Excuse me, but it is accusing **method call expected** because you did not wrote **mContext** but **mContext()** instead, with the parenthesis at the end, giving this the notation of a method, and this is not a method, thus the raising error. – statosdotcom Apr 03 '16 at 01:28
  • Thank you Faraday. Best. and thanks to "someone" for the downvote! God bless. – statosdotcom Apr 03 '16 at 01:35
  • @MarkKeen, I did. Didn't notice any glitch – Faraday Apr 03 '16 at 01:41
  • @Faraday thats surprising as it should disappear if the dialog is open unless you do a bit if work and save its status .. – Mark Apr 03 '16 at 01:47
  • @MarkKeen, I use a Lollipop device. Which SDK version does it disappear? – Faraday Apr 03 '16 at 01:54
  • @Faraday - it shouldn't work full stop .. have a look here : http://stackoverflow.com/questions/7557265/prevent-dialog-dismissal-on-screen-rotation-in-android .. if its working happy days .. don't know why off hand though ... unless you've changed a setting in the manifest `android:configChanges="keyboardHidden|orientation"` .. which BTW don't do! – Mark Apr 03 '16 at 02:00