views:

189

answers:

2

Whenever a application needs internet and connection fails, I get a message dialog

Connection failed
This application requires network access. Enable mobile network or Wi-Fi to download data.

and two buttons, Settings, Cancel.

How do I detect there is no internet connection?
How do I popup a same dialog in my application?

+2  A: 

Here you can find a reference on how to build dialogs

http://developer.android.com/guide/topics/ui/dialogs.html

Here is a code snippet that should help you detecting internet access:

http://www.androidsnippets.org/snippets/131/

btw: wasn't it you that asked this question already on 15. Nov. 2009? http://groups.google.com/group/android-beginners/browse_thread/thread/715cedfc5fd6f020?utoken=QyP-dzQAAACEe9Lph6eUOAakqA3-BnR-KvPfK0ltyCETgVsCM3D7GeEWTAM9S5G8WBs1q2tBppm1FwwMIvCGKnxkm-CrwSdp

Roflcoptr
Yeah that was me, completely forget about that. Thanks. But... it seams this dialog is officially used in many apps, isn't this dialog an inbuilt one?
Pentium10
I'm not sure. Maybe Android checks if you want to access the Internet and you can't and then this dialog appears.Or as this dialogs are more or less all looking the same because they are just customized build-in dialogs
Roflcoptr
Probably it's a HTC Sense thing, as after spending some time to investigate, it appears only on their apps.
Pentium10
yes maybe, but with these tutorials it souldn't be too difficult to do something similar
Roflcoptr
+2  A: 
/**
 * Checks if we have a valid Internet Connection on the device.
 * @param ctx
 * @return True if device has internet
 *
 * Code from: http://www.androidsnippets.org/snippets/131/
 */
public static boolean haveInternet(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null || !info.isConnected()) {
        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return true;
    }
    return true;
}
/**
     * Display a dialog that user has no internet connection
     * @param ctx1
     *
     * Code from: http://osdir.com/ml/Android-Developers/2009-11/msg05044.html
     */
    public static void showNoConnectionDialog(Context ctx1) {
        final Context ctx = ctx1;
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setCancelable(true);
        builder.setMessage(R.string.no_connection);
        builder.setTitle(R.string.no_connection_title);
        builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                return;
            }
        });

        builder.show();
    }
Pentium10