判斷Android網路狀態資訊-wifi,3g/4g 和定位方式-gps,network,passive

來源:互聯網
上載者:User

標籤:android   網路   gps   

我們項目的工具類中,這些方法應該是常駐方法,因為他們太常用了,一方面將這些方法總結在這篇文章中,以便於自己日後使用,另一方面也給大家提供個方便,省的每次到處去找了。

1.判斷當前是否已經連網

不管串連的是WIFI還是移動網路,最終都會返回true,否則返回false

public static boolean isNetworkAvailable(Context context){        if (context!=null) {            //擷取系統服務            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);            //獲得當前網路資訊            NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();            if (activeNetworkInfo!=null) {                //如果已經連網,則會返回true                return activeNetworkInfo.isAvailable();            }        }        return false;    }

還有一種方法,和這個方法返回結果是一樣的

public static boolean isNetworkAvailable(Context context) {        //獲得系統服務,拿到ConnectivityManager        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        if (connectivity == null) {        } else {            //獲得全部網路資訊            NetworkInfo[] info = connectivity.getAllNetworkInfo();            if (info != null) {                //遍曆獲得的網路資訊                for (int i = 0; i < info.length; i++) {                    //網路狀態為CONNECTED時,返回true                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {                        return true;                    }                }            }        }        return false;    }

關於網路狀態NetworkInfo.State有如下總結:

Detailed state Coarse-grained state
IDLE DISCONNECTED
SCANNING CONNECTING
CONNECTING CONNECTING
AUTHENTICATING CONNECTING
CONNECTED CONNECTED
DISCONNECTING DISCONNECTING
DISCONNECTED DISCONNECTED
UNAVAILABLE DISCONNECTED
FAILED DISCONNECTED
詳細狀態 粗略狀態
空閑 未串連
正在搜尋 正在串連中
正在串連中 正在串連中
正在驗證中 正在串連中
已經串連上 已經串連上
正在中斷連線 正在中斷連線
已經中斷連線 未串連
網路不可用 未串連
失敗 未串連
2.判斷當前網路是否是wifi網路
public static boolean isWifi(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetInfo != null                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {            return true;        }        return false;    }
3.判斷當前網路是否是移動網路
public static boolean isMONET(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetInfo != null                && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {            return true;        }        return false;    }
4.判斷裝置的定位方式

Android手機的定位方式有3種模式

1.網路模式:這種模式的定位精度低,定位速度快,耗電量一般,需要許可權

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

2.GPS:這種模式定位精度高,定位速度慢,耗電量大,在室內或GPS訊號差的地方無法定位,需要許可權

android.permission.ACCESS_FINE_LOCATION

3.被動定位:這種模式是通過監聽其他應用程式請求位置服務獲得的位置資訊來擷取位置資訊,無額外耗電量,局限性較大,需要依賴於其他應用程式,需要許可權

android.permission.ACCESS_FINE_LOCATION

我們來看一下如何擷取當前裝置開啟的定位元模式,被動模式是自動開啟的,混合模式是網路模式和gps模式都開啟的情況,這種模式的優點是:定位時間短,精度高。缺點:費電

// 0表示被動模式  1表示網路模式  2表示GPS模式  3表示混合模式    public static int getLocationMode(Context context) {        boolean network = false;        boolean gps = false;        LocationManager locationManager = ((LocationManager) context                .getSystemService(Context.LOCATION_SERVICE));        List<String> accessibleProviders = locationManager.getProviders(true);        for(int i=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&&gps) {            return 3;        }else if (network) {            return 1;        }else if (gps) {            return 2;        }        return 0;    }

著作權聲明:歡迎轉載,轉載請註明出處http://blog.csdn.net/nugongahou110

判斷Android網路狀態資訊-wifi,3g/4g 和定位方式-gps,network,passive

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.