Question

I'm implementing a code that verifies if there is an internet connection. If not, it starts the settings of the WiFi so the user can choose an Internet Connection.

The problem is that i want that when the user choose the connection and click back button, my activity verifies again if there is any connection to continue the execution, but it goes again to the NETWORK_INACTIVE dialog.

Here is the code where i start the new activity:

protected boolean hasNetworkConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    try{
        if (!wifi.isConnected()){
            if(mobile==null || (mobile!=null && !mobile.isConnected())){
                onCreateDialog(NETWORK_INACTIVE).show();
                return false;
            }
        }
    }catch(Exception e){
        Log.e("AEP41-Has Network", ""+e.getStackTrace());
    }
    return true;
}
@Override
    protected Dialog onCreateDialog(int id) {
        AlertDialog.Builder builter = new AlertDialog.Builder(
                AEP41Activity.this);

    switch (id) {

    case NETWORK_INACTIVE:
        builter.setCancelable(false);
        builter.setTitle("Erreur de Reseau");
        builter.setMessage("Aucune connexion internet trouve");
        builter.setNegativeButton("Sortir",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        AEP41Activity.this.finish();
                    }
                });
        builter.setNeutralButton("Choisir Connexion",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(
                                Settings.ACTION_WIFI_SETTINGS));
                });
        break;

    default:
        break;
    }
    return builter.create();
}

I've seen the option startActivityForResult(Intent, int) but i did't found any solution with the use of the Settings.

Is there any way of doing this?

Thanks in advance ;)

Was it helpful?

Solution

This call isn't correct:

onCreateDialog(NETWORK_INACTIVE).show();

You should never call onCreateDialog() yourself. The Android framework does this for you. Instead of calling onCreateDialog(NETWORK_INACTIVE).show(); you should do this:

showDialog(NETWORK_INACTIVE);

OTHER TIPS

I check for an active connection in onResume() and if there isn't one I have a method to create an AlertDialog as follows...

protected void createNetErrorDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
    .setTitle("Unable to connect")
    .setCancelable(false)
    .setPositiveButton("Settings",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(i);
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainActivity.this.finish();
            }
        }
    );
    AlertDialog alert = builder.create();
    alert.show();
}

If the user hits the Cancel button I finish the Activity otherwise start Settings. If the user hits BACK in Settings, onResume() is called again and the network is checked again. This works fine for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top