Determine if the network is connected and determine if the GPS is connected

Source: Internet
Author: User

Determine if the network is connected

public static Boolean isnetworkenabled (context context) {

int Status=-1//Set the default connection status to-1

Connectivitymanager Connectivitymanager = (connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo networkinfo = Connectivitymanager.getactivenetworkinfo ();
if (networkinfo! = null && networkinfo.isconnected ()) { //networkinfo.isconnected () determines whether the network is connected

Switch (Networkinfo.gettype ()) {
Case Connectivitymanager.type_mobile: {
Status = 0;// mobile data connection. when active, all data traffic. The interface that will use this default network type
Break
}
Case Connectivitymanager.type_wifi: {
status = 1;// wireless data connection. when active, all data traffic
Break
}
Default: {
status = 2;// One mms-specific mobile data connection. You can use this network type
Break
}
}
}
if (status = =-1) {
return false;
} else {
return true;
}

}

Determine if the GPS is connected

public static Boolean Isgpsopen (context context) {
Locationmanager Locationmanager = (locationmanager) context
. Getsystemservice (Context.location_service);
Boolean GPS = Locationmanager
. isproviderenabled (Locationmanager.gps_provider);
if (GPS)
{
return true;
}
return false;
}

Determine if the network is connected and determine if the GPS 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.