Hello

I am very new to android development, i have a simple app with a webview, i 
want to check the network status and alert the user if there is no network 
connection.

MainActivity.java:
============================================================================

package dk.zerone.vuc;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

        public final boolean networkCheck() {
                ConnectivityManager connec =  
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                // ARE WE CONNECTED TO THE NET
                if ( connec.getNetworkInfo(0).getState() == 
NetworkInfo.State.CONNECTED ||
                connec.getNetworkInfo(0).getState() == 
NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == 
NetworkInfo.State.CONNECTING ||
                connec.getNetworkInfo(1).getState() == 
NetworkInfo.State.CONNECTED ) {
                        return true;
                } else if ( connec.getNetworkInfo(0).getState() == 
NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == 
NetworkInfo.State.DISCONNECTED  ) {
                        return false;
                }
                return false;
        }

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if(networkCheck()) {
                 
                // INTERNET IS AVAILABLE, DO STUFF..
                 
                } else {
                        AlertDialog alertDialog = new 
AlertDialog.Builder(MainActivity.this).create();
                        alertDialog.setTitle("Reset...");
                        alertDialog.setMessage("R u sure?");

                                alertDialog.setButton("OK", new 
DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int 
which) {
                                } });
                                alertDialog.show();
                }
        
        /* Splash screen */
        // ...
        
        /* WebView */
        WebView webview = new WebView(this);
        webview.setWebViewClient(new WebViewClient()); 

        setContentView(webview);
        
        Uri uri = Uri.parse("http://mobil.vucfyn.dk/mobil";);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
       
}
============================================================================

The above code compiles fine, without any errors or warnings, but the dialog is 
never shown, i have tried airplane mode in the emulator, and disabling the 
network on the computer, no effect.

What am i doing wrong?

Kind regards

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to