-2

i am trying to create a google form based android project, i want the application to close or not display the link if it is not able to connect to internet, here is the code, have tried using exit, but it didnt work, any help will be great.

package tscolari.mobile_sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

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

    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    mainWebView.loadUrl("the url of form");
}

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}


i want something like this

if(mainwebview==null)//not able to load page
{
 //close the application
}
Hasan
  • 36
  • 1
  • 16

2 Answers2

2

Use this to check the internet connection: [Note: This is not my code originally. I took it from a tutorial, but I do not know which one]

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class CheckNetworkConnection {

    public static boolean isConnectionAvailable(Context context) {

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnected()
                    && netInfo.isConnectedOrConnecting()
                    && netInfo.isAvailable()) {
                return true;
            }
        }
        return false;
    }
}

Basically, just do an if(!CheckNetworkConnection.isConnectionAvailable(myContext)) {/* Close App*/ }. To close an Activity, use finish(). To close an entire application, however, read here, as it answers all concerns far better than I could in a simple answer here. Please take note that it's not a good idea to suddenly close the app without input from the user.

Edit: As Xaver Kapeller clarified in a comment, finish() is a good way to close an activity- which will close the entire application as well (unless the Activity was started with startActivityForResult() or something similar)

Community
  • 1
  • 1
DragonJawad
  • 1,846
  • 3
  • 20
  • 28
1

Im checking the internet connection with

public static boolean isInternetAvailable(Context cxt) {
    final ConnectivityManager conMgr = (ConnectivityManager) cxt
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo i = conMgr.getActiveNetworkInfo();

    // if there is no active internet connection
    if (i == null || !i.isConnected() || !i.isAvailable()) {
        return false;
    }

    return true;
}

You can exit the app with finish(), but you should let the user somehow know why have you closed the app.

tibbi
  • 249
  • 4
  • 27
  • If I recall correctly, finish() merely closes the Activity, not the entire app. This here gives more info about closing the entire aplication: [Click meh](http://stackoverflow.com/questions/3279578/close-android-application) – DragonJawad Nov 27 '14 at 18:50
  • (And to everyone else, it's best to do something other than closing the entire application, such as a prompt for enabling the internet in some fashion or at least a little dialog before closing) – DragonJawad Nov 27 '14 at 18:52
  • ye that might be true. But anyway, as that user probably explains (tldr), you should never close an app like that. Just show a dialog, toast or sth that internet is unavailable and theres nothing to do offline, and let the user close the app himself. – tibbi Nov 27 '14 at 18:53
  • trying for checking internet,the above code does not help, sorry – Hasan Nov 27 '14 at 18:57
  • cmon man describe your problems more. Whats not working? always returns true, always false? or? – tibbi Nov 27 '14 at 19:02
  • 1
    @MDragon00 The OS handles the application lifecycle, you cannot do anything else but use `finish()` to close the `Activity` and Android will close the application when it is no longer needed. If you use something else like `System.exit(0)` than you are only going to cause **major** problems. **Never** use `System.exit(0)` or something similar on Android. – Xaver Kapeller Nov 27 '14 at 19:11
  • Oh yeah, good point. I was thinking about a startActivityForResult sort of thinking. Thanks for that clarification~ – DragonJawad Nov 27 '14 at 19:12