D
D
Dmitry2018-09-14 17:01:29
Java
Dmitry, 2018-09-14 17:01:29

How to switch activity when the Internet is lost or not available?

Hello!
How to switch activity when the Internet is lost or not available?
In the absence of the Internet, the following message will appear instead of the page

here
5b9bbdf8a23ed350050459.png

Instead, I'd like to switch to another activity with a message or an inscription about a connection problem.
What is already there:
webview
public class MainActivity extends AppCompatActivity {

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

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new MyWebViewClietn());  
        webView.loadUrl("http://google.com/");
    }
}

TryNetwork class
public class TryNetwork
{
    public static boolean check()
    {
        Runtime localRuntime = Runtime.getRuntime();
        boolean bool = false;
        try
        {
            int i = localRuntime.exec("/system/bin/ping -c 1 8.8.8.8").waitFor();
            if (i == 0) {
                bool = true;
            }
            return bool;
        }
        catch (InterruptedException localInterruptedException)
        {
            localInterruptedException.printStackTrace();
            return false;
        }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
        return false;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-09-14
@AlphaKeloid

Combine catch blocks:
and start a new activity right there:

Intent intent = new Intent(this, SomeOtherActivity.class);
        intent.putExtra("NETWORK_ERROR", "Проблема с подключением");  //это опционально
        startActivity(intent);

https://developer.android.com/training/basics/firs...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question