Permissions:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
The code is as follows:
Package com.example.nettest;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo.State;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.ComponentName;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Mainactivity extends Activity {
Private Connectivitymanager Manager;
Private TextView TV;
StringBuffer sb = new StringBuffer (256);
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
TV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Checknetworkstate ();
}
/**
* Detects if the network is connected
*
* @return
*/
Private Boolean checknetworkstate () {
Boolean flag = false;
Get Network connection Information
Manager = (Connectivitymanager) getsystemservice (Context.connectivity_service);
To determine if the network is connected
if (manager.getactivenetworkinfo () = null) {
Flag = Manager.getactivenetworkinfo (). isavailable ();
}
if (!flag) {
Setnetwork ();
} else {
Isnetworkavailable ();
}
Tv.settext (Sb.tostring ());
return flag;
}
/**
* Setting method is called when the network is not connected
*/
private void Setnetwork () {
Toast.maketext (This, "Wi-Fi is closed!", Toast.length_short). Show ();
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.seticon (R.drawable.ic_launcher);
Builder.settitle ("Network alert Information");
Builder.setmessage ("Network is not available, if you continue, please set up the network first!") ");
Builder.setpositivebutton ("Settings", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Intent Intent = null;
/**
* Determine the version of the phone system! If the API is greater than 10 is 3.0+ because the settings of more than 3.0 are not the same as the settings below 3.0, the method is called differently
*/
if (Android.os.Build.VERSION.SDK_INT > 10) {
Intent = new Intent (
Android.provider.Settings.ACTION_SETTINGS);
} else {
Intent = new Intent ();
ComponentName component = new ComponentName (
"Com.android.settings",
"Com.android.settings.WirelessSettings");
Intent.setcomponent (component);
Intent.setaction ("Android.intent.action.VIEW");
}
StartActivity (Intent);
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
}
});
Builder.create ();
Builder.show ();
}
/**
* The network has been connected, then to determine whether the WiFi connection or GPRS connection set some of their own logical calls
*/
private void isnetworkavailable () {
state GPRS = Manager.getnetworkinfo (connectivitymanager.type_mobile)
. GetState ();
state WiFi = manager.getnetworkinfo (Connectivitymanager.type_wifi)
. GetState ();
if (GPRS = = State.connected | | gprs = = state.connecting) {
Toast.maketext (This, "GPRS is open!", Toast.length_short). Show ();
Sb.append ("\ngprs is open!");
} else {
Sb.append ("\ngprs is closed!");
}
Judge the WiFi status to load ads, if it is GPRS mobile network does not load!
if (WiFi = = state.connected | | wifi = = state.connecting) {
Toast.maketext (This, "Wi-Fi is open!", Toast.length_short). Show ();
Loadadmob ();
Sb.append ("\nwifi is open!");
} else {
Sb.append ("\nwifi is closed!");
}
}
/**
* Load AdMob ads in WiFi status
*/
private void Loadadmob () {
Toast.maketext (Getapplicationcontext (), "Ad is loding:", 1). Show ();
Sb.append ("\nad is loding ...");
}
}