android gps定位工具类,Android原生GPS和网络定位工具类
在应用开发中我们常常需要网络获取位置的方法,这里做一个工具类的封装,使用的是Android原生定位,包含两种方式,一种是原生GPS一种是网络定位的封装实现:
工具类代码如下所示:
package com.vondear.tools.tools;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.app.Activity;
import android.support.v4.content.LocalBroadcastManager;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import com.ta.utdid2.android.utils.SharedPreferenceHelper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
*
* Created by AndyYuan on time at 2019/6/20.
*/
public class GetGPSUtil {
static GetGPSUtil getGPSUtil;
public static GetGPSUtil getInstance() {
if (getGPSUtil == null) getGPSUtil = new GetGPSUtil();
return getGPSUtil;
}
/**
* 获取经纬度
*
* @param context
* @return
*/
private String getLngAndLat(Context context) {
double latitude = 0.0;
double longitude = 0.0;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { //从gps获取经纬度
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "没有GPS权限";
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else {//当GPS信号弱没获取到位置的时候又从网络获取
Toast.makeText(context, "GPS信号弱获取不到", Toast.LENGTH_SHORT).show();
return getLngAndLatWithNetwork(context);
}
} else { //从网络获取经纬度
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
//通过查阅资料好像我们的Android手机要开启过GPS(第一次获取须GPS获取或者其他第三方应用开启使用过GPS)后然后getLastKnownLocation(LoactionManager.NETWORK_PROVIDER)这个方法才不会返回空。
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
return longitude + "," + latitude;
}
//从网络获取经纬度
public String getLngAndLatWithNetwork(Context context) {
double latitude = 0.0;
double longitude = 0.0;
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return "没有网络Internet获取权限";
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
return longitude + "," + latitude;
}
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
}
};
}
android gps定位工具类,Android原生GPS和网络定位工具类相关推荐
- 【Android 逆向】Android 逆向通用工具开发 ( 静态库项目中的网络操作核心类 CNetwork 分析 )
文章目录 一.adabingo 静态库项目中的网络操作核心类 CNetwork 分析 一.adabingo 静态库项目中的网络操作核心类 CNetwork 分析 CNetwork 相关方法分析 : 等 ...
- 自定义工具类-----GPS、网络定位
代码依旧是kotlin编写,java类似 1.在清单文件里添加权限 <!--请求网络权限--> <uses-permission android:name="android ...
- 亲友定位助手--具备实时共享位置的智能手机定位地图软件的设计
本项目的出发点就是针对如何解决位置的实时共享问题,使得人们组队外出游玩时的安全性得到保障,还有让每个家庭成员之间可以随时随地地查看其他成员的位置,特别是让老人.小孩的安全得到家人的实时监护.除此之外, ...
- 新手必备的网络营销工具
作为一位互联网新手,结合实际互联网运营工作,给大家推荐一些能有效提高运营效率的网络营销工具利器,这些工具基本涵盖了互联网运营工作的方方面面. 一.网络营销工具之梳理运营思路 领导布置了一项运营任务,让 ...
- 在 Mac 上使用“网络实用工具”
Mac系统升级后,自带的网络工具位置发生了变化,很多人找不到,本文会教会大家如何找到并使用这个强大的工具. 1.打开系统信息工具 2.点击菜单栏的[窗口],[网络实用工具],即可打开网络使用工具. 在 ...
- 如何选择合适的网络自动化工具
通过网络自动化工具实现网络自动化是所有网络组织的关键.如果没有合适的网络自动化工具,拥有由许多设备组成的大型网络环境的组织将无法执行重要操作,例如按时备份配置.实时跟踪不需要的更改以及遵守行业法规.当 ...
- 博客作为网络营销工具的特点
"博客用户数量增长 博客营销价值显现",这是新竞争力网络营销管理顾问在对美国博客用户数量及其博客应用方式的相关调查数据进行综合分析之后得出的结论. 网上营销新观察在博客营销专题中曾 ...
- 六类、七类网线在千兆网络下的性能测试
2018/12/16 某东买的六类线3米,七类线2米. 六类线适用于千兆网络,七类线适用于万兆网络.六类线没有屏蔽层,可提供至少200MHZ的综合衰减对串扰比及整体250MHZ的带宽.七类线有屏蔽,可 ...
- 网络安全的十大最佳网络扫描工具
在当今世界,网络安全不需要介绍或解释.我们都知道,网络安全一词是指保护任何计算机网络,旨在防止任何人以任何方式访问.操纵或滥用网络.因此,我们有与监视和保护计算机网络有关的规则,政策和说明. 网络扫描 ...
最新文章
- 2019年,自动化机器学习AutoML技术还火吗? | BDTC 2019
- 计算机网络——知识结构体系
- 小功率荧光灯拆解分析
- Java执行字符串中的运算公式
- 你在发表理科学术文章过程中有哪些经验值得借鉴
- java中各种流的详细使用
- 我在编写《微软System Center 2012 R2私有云部署实战》中应用的一些小技巧
- python简单练习 -统计文本词频并用柱状图显示
- Shiro框架--将Shrio的session改成HTTPSession数据
- 力扣每日一刷-144,二叉树前序遍历-递归解法
- javascript css函数一设置/读取对象的属性(Style对象与CurrentStyle对象、getComputedStyle)及其小案例
- 软件测试基础—易用性测试
- java 高效的 httpclient_使用httpclient下载zip的有效方法
- web开发规范 - html书写规范
- linux 内核 addr2line,內核調試 arm-none-linux-gnueabi-addr2line 工具使用
- 仿人机器人的跑步研究学习笔记2之什么是正逆运动学
- MT6573芯片资料集锦芯片MTK6573_环境搭建
- python中property详解
- 深入理解扩展运算符实现原理
- iOS打包错误The operation couldn’t be completed. (AppThinning.StubError error 1.)
热门文章
- 多旋翼油门量与升力关系
- springboot聚合工程讲解与部署
- 程序员如何营销自己?
- nginx配置微前端
- mahout第一篇-----Mahout学习路线图
- hadoop Error: JAVA_HOME is incorrectly set.Please update C:\hadoop-3.0.0\etc\hadoop\hadoop-env.cmd
- 残留的过往伤感QQ日志_说太多,不如沉默
- EF CodeFirst 必须要解决的问题
- 大学计算机基础教程实验答案,大学计算机基础试验教程习题参考答案
- 排序:ORDER BY