繁体   English   中英

在没有可用连接时显示Toast错误并阻止显示Webview

[英]Show toast error and prevent display of webview when no connection is available

当没有可用的连接时,它将显示默认的浏览器错误页面,所要的就是根本不显示任何浏览器错误页面,而是显示一条敬酒消息和一个空白屏幕。

我的代码:

public class EarnFragment extends Fragment {
WebView mWebView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v=inflater.inflate(R.layout.fragment_earn, container, false);
    mWebView = (WebView) v.findViewById(R.id.webview);
    mWebView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());

    return v;
}

}

如果要显示黑屏而不是Webview,则首先需要更改布局文件。 然后您在加载网址之前检查互联网连接。 检查互联网连接时间是否不可用,请隐藏您的Web视图并显示Toast消息和相关布局。 其他查看隐藏Relativelayout noConnection。

第1步:创建这样的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webViewData"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <RelativeLayout
        android:id="@+id/noConnection"
        android:layout_width="match_parent"
        android:background="#000000"
        android:layout_height="match_parent"/>
</RelativeLayout>

</LinearLayout>

第2步:使用此功能检查您的互联网连接。

public static boolean checkInternetConnection(Context context) {
    if (context != null) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected() && cm.getActiveNetworkInfo().isConnectedOrConnecting();
        } else {
            return false;
        }
    } else {
        return false;
    }

}

最后,在代码中执行此操作,然后再加载url,

public class EarnFragment extends Fragment {
WebView mWebView;
RelativeLayout noConnection;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

View v=inflater.inflate(R.layout.fragment_earn, container, false);
mWebView = (WebView) v.findViewById(R.id.webview);
noConnection = (RelativeLayout)v.findViewById(R.id.noConnection);

// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

if(checkInternetConnection(getActivity())){
 noConnection.setVisibility(View.GONE);
 mWebView.setVisibility(View.VISIBLE);
 mWebView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");

}else{
 noConnection.setVisibility(View.VISIBLE);
 mWebView.setVisibility(View.GONE);
}


// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());

return v;
}

步骤1:制作include_error_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ebebeb"
    android:gravity="center"
    android:orientation="vertical">
    <!--android:background="#ebebeb"-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/labelError"
            android:layout_centerHorizontal="true"
            android:src="@drawable/img_icon_error_list" />

        <TextView
            android:id="@+id/labelError"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/err_error_list"
            android:textColor="@color/color_app_font_primary" />

        <ProgressBar
            android:id="@+id/errorProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/labelError"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp"
            android:visibility="gone"/>

    </RelativeLayout>

</LinearLayout>

步骤2:制作片段布局fragment_abc.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:id="@+id/errorView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">

        <include layout="@layout/include_error_list_view" />

    </RelativeLayout>

</RelativeLayout>

步骤3:使用JAVA绑定布局。

public class AbcFragment extends Fragment {

    private final String TAG = "AbcFragment";
    private WebView webView;
    private ProgressDialog progress;
    private RelativeLayout errorView;
    private boolean isShowErrorWiew = false;
    private ProgressDialog progress;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_abc, container, false);
        webView = (WebView) view.findViewById(R.id.webview);
        errorView = (RelativeLayout) view.findViewById(R.id.errorView);
        TextView labelError = (TextView) view.findViewById(R.id.labelError);
        labelError.setText("Application unable to connect with internet.");
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        showProgressBarWithoutHide();

        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isShowErrorWiew = false;
                showProgressBarWithoutHide();
                webView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");
            }
        });
        try {
            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.i(TAG, "Processing webview url click...");
                    view.loadUrl(url);
                    return true;
                }

                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "Finished loading URL: " + url);
                    if (!isShowErrorWiew) {
                        errorView.setVisibility(View.GONE);
                    }
                    hideProgressBar();
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Log.e(TAG, "Error: " + description);
                    isShowErrorWiew = true;
                    errorView.setVisibility(View.VISIBLE);
//                Snackbar.make(webView, getString(R.string.err_process_webView), Snackbar.LENGTH_LONG).show();
                }
            });

            webView.loadUrl("https://demo.hazzardweb.com/easylogin-pro/");

        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        /*Todo Always Pause WebView Because Background video play is violating google policy*/
        webView.onPause();
    }

    public void hideProgressBar() {
             if (progress != null) {
             progress.dismiss();
             }
      }

public void showProgressBarWithoutHide() {


        if (progress == null) {
            progress = new ProgressDialog(getActivity());
            progress.setMessage(getString(R.string.please_wait));
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progress.setIndeterminate(true);
            progress.setCancelable(false);
            progress.show();
        } else if (!progress.isShowing()) {
            progress.show();
        }

}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM