Determine Android network status information-wifi,3g/4g and positioning mode-gps,network,passive

Source: Internet
Author: User

In our project's tool class, these methods should be resident methods, because they are too often used, on the one hand, these methods summarized in this article, in order to use their own later, on the other hand to provide a convenient, save every time to find.

1. Determine whether the network is currently connected

Returns true regardless of whether the connection is WiFi or mobile network, otherwise false

 Public Static Boolean isnetworkavailable(Context context) {if(context!=NULL) {//Get system ServicesConnectivitymanager cm = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);//Get current Network informationNetworkinfo activenetworkinfo = Cm.getactivenetworkinfo ();if(activenetworkinfo!=NULL) {//If the network is already connected, it will return true                returnActivenetworkinfo.isavailable (); }        }return false; }

There is another way, and this method returns the same result.

 Public Static Boolean isnetworkavailable(Context context) {//Get system service, get ConnectivitymanagerConnectivitymanager connectivity = (Connectivitymanager) context.getsystemservice (Context.connectivity_service);if(Connectivity = =NULL) {        }Else{//Get all network informationNetworkinfo[] info = Connectivity.getallnetworkinfo ();if(Info! =NULL) {//traverse the obtained network information                 for(inti =0; i < info.length; i++) {//Returns True when the network status is connected                    if(info[i].getstate () = = NetworkInfo.State.CONNECTED) {return true; }                }            }        }return false; }

About network status Networkinfo.state has the following summary:

detailed State coarse-grained State
IDLE disconnected
scanning connecting
connecting connecting
authenticating connecting
CONNECTED connected
disconnecting disconnecting
disconnected disconnected
unavailable disconnected
FAILED disconnected
Detailed Status Rough State
Free Not connected
are searching Connecting in progress
Connecting in progress Connecting in progress
Verifying in progress Connecting in progress
has been connected to the has been connected to the
Disconnecting connection Disconnecting connection
Already disconnected Not connected
Network not available Not connected
Failed Not connected
2. Determine if the current network is a WiFi network
publicstaticbooleanisWifi(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        ifnull                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {            returntrue;        }        returnfalse;    }
3. Determine if the current network is a mobile network
publicstaticbooleanisMONET(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        ifnull                && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {            returntrue;        }        returnfalse;    }
4. Determine how the device is positioned

Android phones have 3 modes of targeting

1. Network mode: The positioning accuracy of this mode is low, the positioning speed is fast, power consumption is general, need authority

android.permission.ACCESS_COARSE_LOCATION或者android.permission.ACCESS_FINE_LOCATION

2.GPS: This mode positioning accuracy is high, positioning speed, power consumption, indoor or GPS signal poor location, need permission

android.permission.ACCESS_FINE_LOCATION

3. Passive positioning: This mode is the location information obtained by listening to other applications to request location services to obtain location information, no additional power consumption, the limitations are large, need to rely on other applications, the need for permissions

android.permission.ACCESS_FINE_LOCATION

Let's look at how to get the current device to open the positioning mode, the passive mode is automatically opened, mixed mode is the network mode and GPS mode are open, the advantages of this mode is: Short positioning time, high precision. Cons: Fee Electricity

//0 means passive Mode 1 means network Mode 2 means GPS mode 3 means mixed mode     Public Static int Getlocationmode(Context context) {BooleanNetwork =false;BooleanGPS =false;        Locationmanager Locationmanager = ((Locationmanager) context. Getsystemservice (Context.location_service)); list<string> accessibleproviders = Locationmanager.getproviders (true); for(intI=0; I<accessibleproviders.size (); i++) {String type = Accessibleproviders.get (i);if(LocationManager.NETWORK_PROVIDER.equals (type)) {Network =true;Continue; }if(LocationManager.GPS_PROVIDER.equals (type)) {GPS =true;Continue; }        }if(NETWORK&AMP;&AMP;GPS) {return 3; }Else if(Network) {return 1; }Else if(GPS) {return 2; }return 0; }

Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/nugongahou110

Determine Android network status information-wifi,3g/4g and positioning mode-gps,network,passive

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.