public static Boolean isnetworkconnected (context context) {
if (context! = null) {
Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();
if (mnetworkinfo! = null) {
Mnetworkinfo.isavailable ();
Return true;//has a net
}
}
return false;//no net
}
public static void Opennetworksetting (final context context) {
//check for network
Connectivitymanager Connectivityman Ager = (Connectivitymanager) context
. Getsystemservice (context.connectivity_service);
Networkinfo activenetworkinfo = Connectivitymanager
. Getactivenetworkinfo ();
Simulator is to use the computer to surf the Internet, it is possible to set flight mode activenetworkinfo! =null
//In a real-machine test. Real machine also want to surf the Internet
//with a laptop, do WiFi connection
if (activenetworkinfo = = null) {
//no NET, display a dialog,
Alertdialog.builder D Ialog = new Builder (context);
Dialog.setmessage ("Pro, now you have no net");
//Open
Dialog.setpositivebutton ("Open", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
try {
Different Android version network settings interface The Intent-filetr,action in activity is not the same.
Different Android versions of the code are not the same.
int androidversion=android.os.build.version.sdk_int;
Get the phone manufacturer's name through the code,
Different manufacturers of mobile phone code is not the same.
Some mobile phone can get mobile phone number, most mobile phone can get the serial number of the SIM card, serial number of each cell phone is unique.
if (androidversion>=10)
{
Open the system's own network Setup interface
Intent Intent = new Intent (
Android.provider.Settings.ACTION_WIRELESS_SETTINGS);
Context.startactivity (Intent);
}
} catch (Exception e) {
Todo:handle exception
}
}
});
Cancel
Dialog.setnegativebutton ("Cancel", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Dialog.cancel ();
}
});
Dialog.show ();
}
}
Android to determine whether the phone is connected