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)类的基本操作「建议收藏」


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

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

(1)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • uint32 t java_数据类型 — uint32_t 类型「建议收藏」

    uint32 t java_数据类型 — uint32_t 类型「建议收藏」1>.在写程序时注意”无符号类型”的使用,各种类型边界值的情况.如:a>当某个数据不可能为负数时我们一定要考虑用以下类型:unsignedchar,unsignedint,uint32_t,size_t,uint64_t,unsignedlongint,b>当有些数据你不知道是正负时一定不要用”a>”中的类型,不然他永远也不可能为负.c>…

    2022年9月6日
    3
  • navicat15激活码序列号(注册激活)

    (navicat15激活码序列号)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html83PVI25FMO-eyJsaWNlbnNlSW…

    2022年3月27日
    841
  • SpringBoot重点详解–使用JPA操作数据库[通俗易懂]

    SpringBoot重点详解–使用JPA操作数据库[通俗易懂]目录JPA&SpringDataJPA配置Maven依赖配置数据源和JPA创建POJO实体数据持久化使用SpringDataJPA接口(方式一)CrudRepositoryPagingAndSortingRepositoryJpaRepositoryQueryByExampleExecutor自定义查询方法(方式二)JUnit测试…

    2022年6月22日
    26
  • python中求平均值(python调用自定义函数)

    该楼层疑似违规已被系统折叠隐藏此楼查看此楼defma(x,y):”’#自定义函数“ma(x,y)”指南函数格式:ma(x,y)函数功能:求序列数据x的y周期的简单平均值,输出值为序列平均值、即列表。函数使用举例:#输入:L=[1,2,3,4,5]ma(L,3)#输出:[2.0,3.0,4.0]#输出注解:(1+2+3)/3=2.0(2+3+4)/3=3.0(3+4+5)/3=4.0…

    2022年4月12日
    373
  • Mac下Ant安装「建议收藏」

    Mac下Ant安装「建议收藏」首先进入Ant官网(http://ant.apache.org/bindownload.cgi)下载Ant:(本人的默认下载在/User/xx/Download)正常安装过程:1:sudosh(会提示你输入当前用户的密码)2:cp/User/xx/Download/apache-ant.1.9.4-bin.zip/usr/local(拷贝ant压缩包到/user/local目录下)3:c

    2022年7月25日
    19
  • getline与get函数的区别

    getline与get函数的区别

    2022年1月19日
    50

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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