Android to determine whether the phone is connected

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.