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

Asked

Viewed 70 times

0

I’m making a Webview app. It’s pretty simple: test the connection if it actually loads the site; fake case loads another Activity (other than the main).

While there is connection, the app works right, 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 it is possible to observe, case without connection is called a method that implements an Intent that in turn should call ConnectionErrorActivity.class. But that doesn’t happen. Somebody give me a light?

1 answer

2

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

Try this one:

public static boolean isOnline() {

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

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

}
  • Thanks for the help, but it didn’t work out... now it really got into the "Else" of the connection test, but it didn’t load the second Activity, instead it got a bug. Maybe my second Activity is wrong?! - public class connectionErrorActivity extends Mainactivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_connection_error); } }

  • Probably the problem is the other. What is the error message in Logcat?

  • Capturing and Displaying logcat messages from application. This behavior can be disabled in the "Logcat output" Section of the "Debugger" Settings page. D/boost: new Perfservicewrapper D/boost: startActivityForResult boost 2500 E/Multiwindowproxy: getServiceInstance failed! ... the app does not close on mobile and the log keeps repeating this error

  • 1

    I think you better open a new question, put the code only of the other Activity and the full log because we are running away from the scope of this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.