Skip to content Skip to sidebar Skip to footer

Ask User To Start Wifi Or 3g On Launching An Android App If Not Connected To Internet

An android app that accesses some online pages using WebView is what I'm working with. I want to incorporate an option where, if the user is not connected to internet, the applicat

Solution 1:

Create an AlertDialog if there isn't a network connection to tell the user and prompt them.

Use this code as an example...

protectedvoidcreateNetErrorDialog() {

    AlertDialog.Builderbuilder=newAlertDialog.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",
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int id) {
                Intenti=newIntent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(i);
            }
        }
    )
    .setNegativeButton("Cancel",
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
            }
        }
    );
    AlertDialogalert= builder.create();
    alert.show();
}

Creating an Intent with Settings.ACTION_WIRELESS_SETTINGS as an action will start the built-in activity for the network settings.

Post a Comment for "Ask User To Start Wifi Or 3g On Launching An Android App If Not Connected To Internet"