I am wanting to check the status of my Wifi/3G connection, but when my application is closed when I lose my signal connection. I use a code found on the network as an example. Tested in the emulator and when I take the 3G connection the app is forced to close. this code is simple and got the logs.
Log ERROR: http://img826.imageshack.us/img826/2389/hmjz.png
public class Activity1 : Activity
{
int count = 1;
public static TextView txtNote;
static string teste2;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
txtNote = FindViewById<TextView>(Resource.Id.textView1);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
protected override void OnResume()
{
base.OnResume();
IntentFilter mIFNetwork = new IntentFilter();
mIFNetwork.AddAction(Android.Net.ConnectivityManager.ConnectivityAction);
RegisterReceiver(new NetworkChangeReceiver(), mIFNetwork);
}
public class NetworkChangeReceiver : Android.Content.BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
String status = AndroidApplication10.Resources.NetworkUtil.getConnectivityStatusString(context);
Toast.MakeText(context, status, ToastLength.Long).Show();
}
}
}
}
Other Class:
public class NetworkUtil { public static int TYPE_WIFI = 1; public static int TYPE_MOBILE = 0 ; public static int TYPE_NOT_CONNECTED = 3;
public static int getConnectivityStatus(Context context)
{
ConnectivityManager cm = (ConnectivityManager)context
.GetSystemService(Context.ConnectivityService);
NetworkInfo activeNetwork = cm.GetNetworkInfo(ConnectivityType.Mobile);
int teste = (int) activeNetwork.Type;
if (null != activeNetwork)
{
if (teste == NetworkUtil.TYPE_WIFI)
return TYPE_WIFI;
if (teste == NetworkUtil.TYPE_MOBILE)
return TYPE_MOBILE;
}
return TYPE_NOT_CONNECTED;
}
public static String getConnectivityStatusString(Context context)
{
int conn = NetworkUtil.getConnectivityStatus(context);
String status = null;
if (conn == NetworkUtil.TYPE_WIFI)
{
status = "Wifi enabled";
}
else if (conn == NetworkUtil.TYPE_MOBILE)
{
status = "Mobile data enabled";
}
else if (conn == NetworkUtil.TYPE_NOT_CONNECTED)
{
status = "Not connected to Internet";
}
return status;
}
}