Android,androidadt

來源:互聯網
上載者:User

Android,androidadt
位置定位(Location)服務(Service)類的基本操作


本文地址: http://blog.csdn.net/caroline_wendy


定位服務(Location Service),可以確定行動裝置的地址,在地圖相關服務中,經常會使用GPS和移動相關的兩種定位服務,GPS較為精準。

根據常用的定位服務功能,又添加網路檢測和Wifi檢測,和啟動系統設定介面進行測試的功能。


代碼:

import android.content.Context;import android.content.Intent;import android.location.LocationManager;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.wifi.WifiManager;import android.provider.Settings;/** * Created by wangchenlong on 14-11-17. * * 定位服務的庫: * 包含功能:判斷是否啟動 定位服務、網路連接、WIFI串連 * 頁面跳轉-> 定位服務設定介面,WIFI設定介面 */public class LocationServiceUtils {    private static final String TAG = "LocationServiceUtils";    /**     * 判斷是否啟動定位服務     *     * @param context 全域資訊介面     * @return 是否啟動定位服務     */    public static boolean isOpenLocService(final Context context) {        boolean isGps = false; //判斷GPS定位是否啟動        boolean isNetwork = false; //判斷網路定位是否啟動        if (context != null) {            LocationManager locationManager                    = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);            if (locationManager != null) {                //通過GPS衛星定位,定位層級可以精確到街(通過24顆衛星定位,在室外和空曠的地方定位準確、速度快)                isGps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);                //通過WLAN或移動網路(3G/2G)確定的位置(也稱作AGPS,輔助GPS定位。主要用於在室內或遮蓋物(建築群或茂密的深林等)密集的地方定位)                isNetwork = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);            }            if (isGps || isNetwork) {                return true;            }        }        return false;    }    /**     * 判斷是否啟動全部網路連接,包括WIFI和流量     *     * @param context 全域資訊介面     * @return 是否串連到網路     */    public static 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;    }    /**     * 判斷是否啟動WIFI串連     *     * @param context 全域資訊介面     * @return 是否串連到WIFI     */    public static boolean isWifiConnected(Context context) {        if (context != null) {            WifiManager wifi = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);            if (wifi != null) {                return wifi.isWifiEnabled();            }        }        return false;    }    /**     * 跳轉定位服務介面     *     * @param context 全域資訊介面     */    public static void gotoLocServiceSettings(Context context) {        final Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(intent);    }    /**     * 跳轉WIFI服務介面     *     * @param context 全域資訊介面     */    public static void gotoWifiServiceSettings(Context context) {        final Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        context.startActivity(intent);    }}


工具類的靜態方法可以直接使用。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.