Android – 位置定位(Location)服务(Service)类的基本操作「建议收藏」

Android – 位置定位(Location)服务(Service)类的基本操作

大家好,又见面了,我是全栈君。

位置定位(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);
    }

}

工具类的静态方法能够直接使用。



Android - 位置定位(Location)服务(Service)类的基本操作「建议收藏」


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116593.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(1)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号