L
L
lamoshnik2019-01-07 03:43:26
Java
lamoshnik, 2019-01-07 03:43:26

How to close completely Android application in the absence of an Internet connection automatically?

It is necessary to automatically close the application if there is no Internet in order to avoid the WebView error (Page is not available)
Something doesn’t work for me .. (I
post java code:

public class MainActivity extends AppCompatActivity {


    private WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        SimpleWebViewClientImpl webViewClient = new SimpleWebViewClientImpl(this);
        webView.setWebViewClient(webViewClient);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                webView.loadUrl("javascript:window.HtmlViewer.showHTML" +
                        "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });


        webView.loadUrl("https://itcreator.pro/chat/m/");



    }

    class MyJavaScriptInterface {

        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        public void showHTML(String html) {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                    .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();

        }

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
            this.webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }



}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmtm, 2019-01-07
@Dmtm

instead of closing the application, isn't it better to check if there is internet beforehand?
before trying to download something

D
Denis Zagaevsky, 2019-01-07
@zagayevskiy

What heresy? Would you yourself use an application that silently closes at a random moment in time?
You can monitor the Internet and that everything has loaded, and if not, then show the user a clear message about it. To close (perhaps forever) such an application is entirely up to the user himself.

G
garastard, 2019-01-09
@garastard

Absolutely not user friendly.
You can't do that.
It is better to subscribe to the state "there is Internet\no Internet" and, in the absence of it, notify the user that without Internet users your application becomes absolutely useless to him. By means of toast\popup\alertDailog

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question