Answer the question
In order to leave comments, you need to log in
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
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/");
}
}
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
Combine catch blocks:
and start a new activity right there:
Intent intent = new Intent(this, SomeOtherActivity.class);
intent.putExtra("NETWORK_ERROR", "Проблема с подключением"); //это опционально
startActivity(intent);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question