Android layout 有效的显示方式';无法接入网络';使用'查看;重试';以及其他选项,如';Wifi设置'';移动网络&x27;

Android layout 有效的显示方式';无法接入网络';使用'查看;重试';以及其他选项,如';Wifi设置'';移动网络&x27;,android-layout,android-fragments,view,loading,android-volley,Android Layout,Android Fragments,View,Loading,Android Volley,我需要将此包含在我的应用程序中 什么时候触发 用户从UI触发的任何操作都需要internet,而不需要internet 任何需要internet的后台活动(如异步加载图像)都没有internet 不当应用程序处于空闲状态或后台/用户操作不需要互联网时 我试过什么 我用凌空抽射。每次我写一个JsonObjectRequest 问题:重复太多,AlertDialog代码行太多 添加了一个名为Alerts的通用类,并从中调用了AlertDialog DialogInterface.OnClick

我需要将此包含在我的应用程序中

什么时候触发

  • 用户从UI触发的任何操作都需要internet,而不需要internet
  • 任何需要internet的后台活动(如异步加载图像)都没有internet
  • 当应用程序处于空闲状态或后台/用户操作需要互联网时
我试过什么

  • 我用凌空抽射。每次我写一个
    JsonObjectRequest
  • 问题:重复太多,AlertDialog代码行太多

  • 添加了一个名为
    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);
    
  • 我的班级

        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();
            }
        }
    
    问题
    再试一次
    按钮不能满足我的要求。嗯,假设有人对此进行了修复,我可以说,它只是用5行代码替换了大约30行代码,那么,转换呢?我需要说
    连接
    ,直到收到回复

  • 这次,我添加了一个名为
    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);
    
    jsonObjectRequest=newjsonobjectrequest(Request.Method.POST,getString(R.string.api\u root\u path)+“/profile”,getJson(),new Response.Listener()){
    @凌驾
    公共void onResponse(JSONObject响应){
    试一试{
    if(response.getBoolean(“success”)){
    动画淡出(加载布局,500);
    动画.fadeIn(主布局,500);
    JSONObject dataJson=response.getJSONObject(“数据”);
    JSONObject userJson=dataJson.getJSONObject(“用户”);
    name.setText(userJson.getString(“name”));
    dob.setText(userJson.getString(“dob”).equals(“null”)?null:userJson.getString(“dob”);
    email.setText(userJson.getString(“email”);
    setText(userJson.getString(“mobile_no”);
    promotionOffers.setChecked(userJson.getBoolean(“offers”);
    }否则{
    Alerts.RequestUnauthorizeAlert(ProfileActivity.this);
    System.out.println(response.getString(“error”);
    }
    }catch(JSONException e){e.printStackTrace();}
    }
    },new Response.ErrorListener(){
    @凌驾
    公共无效onErrorResponse(截击错误){
    DialogInterface.OnClickListener onClickTryAgain=新建DialogInterface.OnClickListener(){
    @凌驾
    public void onClick(DialogInterface dialog,int which){
    getInstance(ProfileActivity.this).addToRequestQueue(jsonObjectRequest);
    }
    };
    if(TimeoutError的错误实例)Alerts.timeoutErrorAlert(ProfileActivity.this,onClickTryAgain);
    else if(错误实例of NoConnectionError)Alerts.InternetConnectionErroralerts(ProfileActivity.this,onClickTryAgain);
    else Alerts.unknownErrorAlert(ProfileActivity.this);
    System.out.println(“响应错误:+错误”);
    }
    });
    动画.fadeIn(加载布局,500);
    动画。淡出(主布局,500);
    getInstance(this).addToRequestQueue(jsonObjectRequest);
    
    假设
    Animations.fadeIn(视图,int-ms)
    动画。淡出(视图,int-ms)已正确定义并执行此任务

    问题:在所有设备中重复布局并编写代码使其褪色的痛苦

  • 了解了
    ViewStub
    s。以编程方式生成代码,以获得相同的
    ConnectingLayout
    。现在我可以更改文本,如保存、加载等
  • 问题:不是一个很大的飞跃,但是。。,这是我刚刚踏上的又一块垫脚石

  • 我不需要说它正在连接或加载,我只需要说‘嘿,你的手机正在处理一些事情’。所以我想为什么不在s中使用加载循环呢
  • 问题:太好了,上面显示的加载圆形缺口很小,如果加载失败,我可以调用警报,但是在哪里打开wifi、打开3G和其他东西,对于
    警报对话框
    来说太大了,无法容纳所有这些,是否有其他方式显示
    重试

  • 查看了在所有活动中使用
    相同片段以及
    重定向到callActivityForResult
    (很抱歉,我不能添加两个以上的链接)
  • 问题:嗯。。。!我发现,将用户带到一个新的活动会消耗资源,并大大降低性能。碎片呢

    我不会放弃!我知道我正在艰难地学习,但我非常希望开发者社区能帮助我


    我遇到了同样的需求和问题。我正在开发一个基于截取的库,在这个库中,我处理的大多数问题都是rxJava全局可观察的,当然还有带有静态警报/视图生成器的警报类。连接处理我试图使它顺利使用和概念

    Volley一旦从服务器接收到内容,就会删除所有侦听器,无论响应是正确响应还是错误响应

    每次应该发送或重新发送请求时,都需要构造请求

    这些步骤将有助于实现这一目标

    • 使用
      重试
      和其他必要的按钮创建片段,如
      无线设置
      移动网络设置