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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 工作笔记

    工作笔记

    2021年11月29日
  • pytest的使用_java中方法的调用

    pytest的使用_java中方法的调用Pytest执行用例规则Pytest在命令行中支持多种方式来运行和选择测试用例1.对某个目录下所有的用例pytest2.对模块中进行测试pytesttest_mod.py3.对文件夹进行

    2022年7月30日
  • 基于epoll的TP传输层实现

    1.抽象TP传输层设计在使用epoll实现实际的传输层之前,先设计一个抽象的传输层,这个抽象的传输层是传输层实现的接口层。接口层中一共有以下几个通用的类或者接口:(1)Socket:通用的套接

    2021年12月28日
  • 常见邮件发送失败原因分析以及解决方法是_手机邮箱发送失败

    常见邮件发送失败原因分析以及解决方法是_手机邮箱发送失败一.发到sina.com的邮件会被退回二.发到tom.com的邮件会被退回三.发到aol.com的邮件会被退回五.收到退信错误提示为”5545.7.1Rejectedxxx.xxx.xxx.xxxfoundindnsbl.sorbs.net”,怎么办?六。为什么直接发新邮件到hotmail.com不成功,而回复hotmail.com的邮件可以成功?七.发给Yahoo的邮件出现"553Mailfrom61.185.81.172notallowed-VS99-I.

    2022年10月7日
  • PyQuery 库

    PyQuery 库强大又灵活的网页解析库。如果你觉得正则写起来太麻烦,BearutifulSoup语法太难记,而又熟悉jQuery的语法,那么PyQuery就是你的绝佳选择1、初始化1.1、字符串初始化html="""<div><ul><liclass="item-0">firstitem</li>

    2022年5月18日
  • smokeping网络监控

    smokeping网络监控一、smokeping介绍我们在选择机房的时候,如何知道这个机房的网络情况,这个时候就要用到网络监控软件:smokeping本文主要介绍如何使用smokeping来监控idc机房的网络质量情况,从

    2022年7月3日

发表回复

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

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