How to call a new Activity through a conditional structure that tests the connection?

0

I'm making a WebView app. It's quite simple: I test the connection truth case loads the site; false case loads another activity (other than main).

While there is a connection, the app works fine loading the site. But when there is no connection it returns the error common to any browser, when it was to return my second activity that says the device is without internet.

Here's my MainActivity code:

public class MainActivity extends Activity {

    WebView wv;
    String URL = "http://www.google.com";

    @Override
    public void onBackPressed() {
        if(wv.canGoBack() ){
            wv.goBack();
        }else{
            super.onBackPressed();
        }
    }

    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if(cm == null) {
            return false;
        } else
            return true;

    }

    public void notConnected(){
        Intent it = new Intent(MainActivity.this, connectionErrorActivity.class);
        startActivity(it);

    }

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

            wv.getSettings().setJavaScriptEnabled(true);
            wv.setFocusable(true);
            wv.setFocusableInTouchMode(true);

            wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
            wv.getSettings().setDomStorageEnabled(true);
            wv.getSettings().setDatabaseEnabled(true);
            wv.getSettings().setAllowFileAccess(true);
            wv.getSettings().setAppCacheEnabled(true);
            wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

            wv.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String URL, String userAgent, String contentDisposition,
                                            String mimetype, long contentLength) {
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));

                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Boletos RNet");
                    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                    dm.enqueue(request);
                    Toast.makeText(getApplicationContext(), "Fazendo Download", Toast.LENGTH_LONG).show();
                }
            });

            wv.loadUrl(URL);
            wv.setWebViewClient(new WebViewClient());

        }else {
            notConnected();
        }
    }
}

As you can see, a connectionless method is called that implements an Intent which in turn should call ConnectionErrorActivity.class . But that does not happen. Someone give me a light?

    
asked by anonymous 11.01.2018 / 23:12

1 answer

1

Your problem is that your isOnline() method does not work. It always returns true .

Try this one here:

public static boolean isOnline() {

    ConnectivityManager cm =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null && activeNetwork.isConnectedOrConnecting();

}
    
11.01.2018 / 23:22