Obtaining network information requires that the appropriate permissions be added to the Androidmanifest.xml file.
1) Determine if there is an Internet connection
[Java]View PlainCopyprint?
- Public Boolean isnetworkconnected (context context) {
- if (context! = null) {
- Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
- . Getsystemservice (Context.connectivity_service);
- Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();
- if (mnetworkinfo! = null) {
- return mnetworkinfo.isavailable ();
- }
- }
- return false;
- }
2) determine if the WiFi network is available
[Java]View PlainCopyprint?
- Public Boolean iswificonnected (context context) {
- if (context! = null) {
- Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
- . Getsystemservice (Context.connectivity_service);
- Networkinfo Mwifinetworkinfo = Mconnectivitymanager
- . Getnetworkinfo (Connectivitymanager.type_wifi);
- if (mwifinetworkinfo! = null) {
- return mwifinetworkinfo.isavailable ();
- }
- }
- return false;
- }
3) Determine if the mobile network is available
[Java]View PlainCopyprint?
- Public Boolean ismobileconnected (context context) {
- if (context! = null) {
- Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
- . Getsystemservice (Context.connectivity_service);
- Networkinfo Mmobilenetworkinfo = Mconnectivitymanager
- . Getnetworkinfo (Connectivitymanager.type_mobile);
- if (mmobilenetworkinfo! = null) {
- return mmobilenetworkinfo.isavailable ();
- }
- }
- return false;
- }
4) Get the type information for the current network connection
[Java]View PlainCopy print?
- Public static int Getconnectedtype (context context) {
- if (context! = null) {
- Connectivitymanager Mconnectivitymanager = (connectivitymanager) context
- . Getsystemservice (Context.connectivity_service);
- Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();
- if (mnetworkinfo! = null && mnetworkinfo.isavailable ()) {
- return Mnetworkinfo.gettype ();
- }
- }
- return-1;
- }
Android Networkinfo Class