An effective way to display the “Cant access network” View with “Retry” and additional options such as “Wi-Fi Settings”, “Mobile Network”,

I need to include this in my application

No connection

When does it fire?

  • Any action initiated by the user from the user interface requires the Internet and no Internet
  • Any background actions (such as asynchronous image downloads) that require the Internet do not have Internet access.
  • NOT when the application is in standby mode or when background / user actions are not performed that require access to the Internet

What have i tried?

  • I am using Volley . Launched AlertDialoginside Response.ErrorListenerVolley, every time I wrote aJsonObjectRequest

: , AlertDialog

  1. Alerts AlertDialog ,

    DialogInterface.OnClickListener onClickTryAgain = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Swift.getInstance(ProfileActivity.this).addToRequestQueue(jsonObjectRequest);
        }
    };
    if(error instanceof TimeoutError) Alerts.timeoutErrorAlert(ProfileActivity.this, onClickTryAgain);
    else if(error instanceof NoConnectionError) Alerts.internetConnectionErrorAlert(ProfileActivity.this, onClickTryAgain);
            else Alerts.unknownErrorAlert(ProfileActivity.this);
    

My Alerts.class

    public class Alerts {


        public static void internetConnectionErrorAlert(final Context context, DialogInterface.OnClickListener onClickTryAgainButton) {
            String message = "Sometimes the internet gets a bit sleepy and takes a nap. Make sure its up and running then we'll give it another go";
            new AlertDialog.Builder(context)
                    .setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle("Network Error")
                    .setMessage(message)
                    .setPositiveButton("Try Again", onClickTryAgainButton)
                    .setNegativeButton("Turn Internet On", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent i = new Intent(Settings.ACTION_SETTINGS);
                            ((Activity) context).startActivityForResult(i, 0);
                        }
                    })
                    .show();
        }

        public static void timeoutErrorAlert(Context context, DialogInterface.OnClickListener onClickTryAgainButton) {
            String message = "Are you connected to internet? We guess you aren't. Turn it on and we'll rock and roll!";
            new AlertDialog.Builder(context)
                    .setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle("Network Error")
                    .setMessage(message)
                    .setPositiveButton("Try Again", onClickTryAgainButton)
                    .show();
        }

        public static void unknownErrorAlert(Context context) {
            new AlertDialog.Builder(context)
                    .setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle("Server Error")
                    .setMessage("We all have bad days! We'll fix this soon...")
                    .setPositiveButton("Hmm, I understand", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    })
                    .show();
        }
    }

: Try again , . , , , - , , 30 5 , ? connecting,

  1. ConnectingLayout, .

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:id="@+id/loading_layout"
        android:visibility="gone">
    
        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading"
            android:gravity="center_horizontal"
            android:textSize="25sp"
            android:fontFamily="sans-serif-light"
            android:layout_margin="5dp"
            android:singleLine="true" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/loading_description"
            android:gravity="center_horizontal"
            android:fontFamily="sans-serif-light"
            android:layout_margin="5dp"
            android:singleLine="true" />
    </LinearLayout>
    

    jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, getString(R.string.api_root_path) + "/profile", getJson(), new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                if(response.getBoolean("success")) {
                    Animations.fadeOut(loadingLayout,500);
                    Animations.fadeIn(mainLayout,500);
                    JSONObject dataJson = response.getJSONObject("data");
                    JSONObject userJson = dataJson.getJSONObject("user");
                    name.setText(userJson.getString("name"));
                    dob.setText(userJson.getString("dob").equals("null")?null:userJson.getString("dob"));
                    email.setText(userJson.getString("email"));
                    phone.setText(userJson.getString("mobile_no"));
                    promotionOffers.setChecked(userJson.getBoolean("offers"));
                } else {
                    Alerts.requestUnauthorisedAlert(ProfileActivity.this);
                    System.out.println(response.getString("error"));
                }
            } catch (JSONException e) { e.printStackTrace(); }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DialogInterface.OnClickListener onClickTryAgain = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    VolleyBaseClass.getInstance(ProfileActivity.this).addToRequestQueue(jsonObjectRequest);
                }
            };
            if(error instanceof TimeoutError) Alerts.timeoutErrorAlert(ProfileActivity.this, onClickTryAgain);
            else if(error instanceof NoConnectionError) Alerts.internetConnectionErrorAlert(ProfileActivity.this, onClickTryAgain);
            else Alerts.unknownErrorAlert(ProfileActivity.this);
            System.out.println("Response Error: " + error);
        }
    });
    Animations.fadeIn(loadingLayout, 500);
    Animations.fadeOut(mainLayout, 500);
    VolleyBaseClass.getInstance(this).addToRequestQueue(jsonObjectRequest);

, Animations.fadeIn(View view, int ms); Animations.fadeOut(View view, int ms); .

:

  1. ViewStub s. , ConnectingLayout. , , ..

: , ....., ,

  1. , : ", -". , SwipeRefreshLayout?

. , , , , , Wi-Fi, 3G , AlertDialog, , Try again?

  1. same fragment in all activities redirecting to a callActivityForResult (, )

: ...! - , . ?

! , , , .

+2
2

Volley DETROYS , - , , .

, .

.

  • Retry , Wifi Settings, Mobile Network Settings
  • FrameLayout RelativeLayout .
  • , FrameLayout , ,
0

. Volley jus, , , rxJava , , Alerts /. ConnectivityManager NoConnectionPolicy.

0

Source: https://habr.com/ru/post/1627357/


All Articles