Determine if your Android phone is connected

Source: Internet
Author: User

Android app Development in most cases to connect with the network, how to determine whether the network is connected, what kind of connection? Here is a way to write about the connection, you can make the appropriate changes according to the requirements:

Private String Connection (context context) {
Determine the network, be sure to remember write permission <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></ Uses-permission>
String connectinfo = "Network Not Connected";
Connectivitymanager connect = (Connectivitymanager) context
. Getsystemservice (Context.connectivity_service);
Networkinfo info = Connect.getactivenetworkinfo ();
if (info! = null && info.isconnected ()) {
if (info.isavailable ()) {//This judgment is used with an exception and needs to be handled
Connectinfo = "Network is connected,";

Whether the GPS is turned on
if (isgpsenabled (this)) {
Connectinfo + = "GPS has been turned on";
}else {
Connectinfo + = "GPs not turned on";
}

Does WiFi Open
if (iswifienabled (This,connect)) {
Connectinfo + = "WiFi on";
}else {
Connectinfo + = "WiFi not turned on";
}

int netType = Info.gettype ();
if (NetType = = Connectivitymanager.type_wifi) {
Connectinfo + = "\ n currently using WiFi";
}else if (NetType = = Connectivitymanager.type_mobile) {
Connectinfo + = "\ n currently using mobile connection";
}else {
Connectinfo + = "\ n currently used connection is:" +nettype;
}
}
return connectinfo;
}

Determine if the GPS is open
public static Boolean isgpsenabled (context context) {
Locationmanager lm = ((Locationmanager) context
. Getsystemservice (Context.location_service));
list<string> accessibleproviders = Lm.getproviders (true);
return accessibleproviders! = null && accessibleproviders.size () > 0;
}
Determine if WiFi is turned on
public static Boolean iswifienabled (Context Context,connectivitymanager mgrconn) {
Telephonymanager Mgrtel = (telephonymanager) context
. Getsystemservice (Context.telephony_service);
Return ((Mgrconn.getactivenetworkinfo ()! = null && mgrconn
. Getactivenetworkinfo (). GetState () = = NetworkInfo.State.CONNECTED) | | Mgrtel
. Getnetworktype () = = Telephonymanager.network_type_umts);
}

Determine if your Android phone is connected

Related Article

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.