Mehdiway
Mehdiway

Reputation: 10636

Android - Make user connect to the internet

In my Android application, I have a logon task in the opening of the app that needs internet connection to get some data, I'm able to test if there's an internet connection thanks to the ConnectivityManager class, but I also want to make the user, or at least suggest him to connect to the internet. Is there any way I can do that ?

Upvotes: 1

Views: 73

Answers (2)

Brianjs
Brianjs

Reputation: 2239

I would create an alert that says they must connect to interenet for application to run. Then open the wireless settings on button select

public static void showNoConnectionDialog(Context _context) {
        final Context context = _context;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(true);
        builder.setMessage("WiFi Connection is needed for app to run");
        builder.setTitle("Not Connected to WiFi");
        builder.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnCancelListener() {
            public void onClick(DialogInterface dialog, int which) {
               //Close out of app or something
            }
        });

        builder.show();
    }

Upvotes: 2

Bryan Herbst
Bryan Herbst

Reputation: 67189

You can launch an intent to go to the device's settings with:

Intent settingsIntent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(settingsIntent);

You can find other similar Intent actions in the android.provider.Settings class.

Upvotes: 1

Related Questions