android question
android question

Reputation: 137

ask user to connect to internet or quit app (android)

i am working on an image gallery app, in which application is retrieving images from internet.

so i want to prompt a dialog-box to ask user to connect to internet or quit application.

show user both WiFi and Carrier network option.

Upvotes: 10

Views: 9462

Answers (5)

Michele La Ferla
Michele La Ferla

Reputation: 6884

This is the way it is done.

This class checks for a valid internet connection:

public class ConnectionStatus {

    private Context _context;

    public ConnectionStatus(Context context) {
        this._context = context;
    }

    public boolean isConnectionAvailable() {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }
}

The following method opens the wi-fi panel if there is no valid internet connection:

public void addListenerOnWifiButton() {
        Button btnWifi = (Button)findViewById(R.id.btnWifi);

        iia = new ConnectionStatus(getApplicationContext());

        isConnected = iia.isConnectionAvailable();
        if (!isConnected) {
            btnWifi.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                    Toast.makeText(getBaseContext(), "Please connect to a hotspot",
                            Toast.LENGTH_SHORT).show();
                }
            });
        }
        else {
            btnWifi.setVisibility(4);
            warning.setText("This app may use your mobile data to update events and get their details.");
        }
    }

The following method opens the 3G panel if there is no valid internet connection:

public void addListenerOn3GButton() {
    Button btnThreeGee = (Button)findViewById(R.id.btn3G);
    iia = new ConnectionStatus(getApplicationContext());

    isConnected = iia.isConnectionAvailable();
    if (!isConnected) {
        btnThreeGee.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
            Toast.makeText(getBaseContext(), "Please check 'Data enabled' option",
                    Toast.LENGTH_SHORT).show();
        }
    });
    }
    else {

         btnThreeGee.setVisibility(4);
         cont.setVisibility(View.VISIBLE);
         warning.setText("This app may use your mobile data to update events and get their details.");
        }
}

Hope this helps :)

Upvotes: 0

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

//Checking For Internet Connection
ConnectionDetector cd = new ConnectionDetector(getApplicationContext()); 

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Connect to wifi or quit")
       .setCancelable(false)
       .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() {

               public void onClick(DialogInterface dialog, int id) {
                   startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
               }
        })
      .setNegativeButton("Quit", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
               this.finish();
         }
      });
      AlertDialog alert = builder.create();
      alert.show();

    return;
    }

Upvotes: 1

Sagar Pilkhwal
Sagar Pilkhwal

Reputation: 3993

Try this:

public static boolean isConnected(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if ((wifiInfo != null && wifiInfo.isConnected()) || (mobileInfo != null && mobileInfo.isConnected())) {
        return true;
}else{
        showDialog();
         return false;
}

private void showDialog()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Connect to wifi or quit")
        .setCancelable(false)
        .setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
           }
         })
        .setNegativeButton("Quit", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                this.finish();
           }
        });
         AlertDialog alert = builder.create();
         alert.show();
}

Upvotes: 5

Anirudh Sharma
Anirudh Sharma

Reputation: 7974

this checks for both wifi and mobile data..run tis code on splash or your main activity to check network connection.popup the dialog if the net is not connected and finish the activity.It's that simple

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}

Upvotes: 6

MobileMon
MobileMon

Reputation: 8651

First you should check if they are already connected or not (tons of examples how to do this online)

If not, then use this code

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Connect to wifi or quit")
.setCancelable(false)
.setPositiveButton("Connect to WIFI", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
   }
 })
.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
        this.finish();
   }
});
 AlertDialog alert = builder.create();
 alert.show();

Upvotes: 3

Related Questions