簡體   English   中英

沒有互聯網顯示對話框

[英]No internet display dialog

我有一個應用程序,它有兩種類型的“沒有互聯網錯誤”。 當您啟動該應用程序時,它會查看您是否有互聯網並顯示錯誤No Internet ,並且我在我的 webviews 中有一個自定義頁面的loadURL

我想隱藏webview網站,但是使用自定義頁面,需要一點時間,你可以看到錯誤+網站。

有可能為我的需要做點什么嗎?

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

        View view=inflater.inflate(R.layout. fragment_paginainicio, container, false);
        final WebView mWebView = (WebView) view.findViewById(R.id.webView_websitepage);
        mWebView.loadUrl("https://google.pt");
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                mWebView.loadUrl("file:///android_asset/errorpage.html");

            }

        });

You have to check if internet is available. Below code will help in that case. If it is not available you can simple make webview visiblity to gone or do not load anything and show alert dialog for no network 

public static boolean isNetworkAvailable(Context mContext) {

                ConnectivityManager connectivityManager
                        = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                return activeNetworkInfo != null && activeNetworkInfo.isConnected();

        }

這個沒有互聯網檢測有點棘手。 在我了解到這一點之前,我一直在掙扎:

在某些情況下,您已連接到路由器或您的數據連接已打開,但實際上您沒有收到任何包裹返回。 由於某種原因,互聯網提供商可能已關閉,或者您沒有支付賬單,他們切斷了您的聯系。

有一個簡單的示例說明我如何檢查是否有 Internet。

public boolean isInternetStable() {
    try {
        return new getInternetStabilityStatus().execute().get();
    } catch (InterruptedException e) {
        return false;
    } catch (ExecutionException e) {
        return false;
    }
}
public class getInternetStabilityStatus extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM