android 获取粗略位置_在Android上获取用户当前位置的最简单,最强大的方法是什么?...
在搜索最佳实现后,如何获得最佳的精确用户位置,我设法结合所有最好的方法,并提出以下类:/**
* Retrieve accurate location from GPS or network services.
*
*
* Class usage example:
*
* public void onCreate(Bundle savedInstanceState) {
* ...
* my_location = new MyLocation();
* my_location.init(main.this, locationResult);
* }
*
*
* public LocationResult locationResult = new LocationResult(){
* @Override
* public void gotLocation(final Location location){
* // do something
* location.getLongitude();
* location.getLatitude();
* }
* };
*/class MyLocation{
/**
* If GPS is enabled.
* Use minimal connected satellites count.
*/
private static final int min_gps_sat_count = 5;
/**
* Iteration step time.
*/
private static final int iteration_timeout_step = 500;
LocationResult locationResult;
private Location bestLocation = null;
private Handler handler = new Handler();
private LocationManager myLocationManager;
public Context context;
private boolean gps_enabled = false;
private int counts = 0;
private int sat_count = 0;
private Runnable showTime = new Runnable() {
public void run() {
boolean stop = false;
counts++;
System.println("counts=" + counts);
//if timeout (1 min) exceeded, stop tying
if(counts > 120){
stop = true;
}
//update last best location
bestLocation = getLocation(context);
//if location is not ready or don`t exists, try again
if(bestLocation == null && gps_enabled){
System.println("BestLocation not ready, continue to wait");
handler.postDelayed(this, iteration_timeout_step);
}else{
//if best location is known, calculate if we need to continue to look for better location
//if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec)
if(stop == false && !needToStop()){
System.println("Connected " + sat_count + " sattelites. continue waiting..");
handler.postDelayed(this, iteration_timeout_step);
}else{
System.println("#########################################");
System.println("BestLocation finded return result to main. sat_count=" + sat_count);
System.println("#########################################");
// removing all updates and listeners
myLocationManager.removeUpdates(gpsLocationListener);
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeGpsStatusListener(gpsStatusListener);
sat_count = 0;
// send best location to locationResult
locationResult.gotLocation(bestLocation);
}
}
}
};
/**
* Determine if continue to try to find best location
*/
private Boolean needToStop(){
if(!gps_enabled){
return true;
}
else if(counts <= 4){
return false;
}
if(sat_count
//if 20-25 sec and 3 satellites found then stop
if(counts >= 40 && sat_count >= 3){
return true;
}
return false;
}
}
return true;
}
/**
* Best location abstract result class
*/
public static abstract class LocationResult{
public abstract void gotLocation(Location location);
}
/**
* Initialize starting values and starting best location listeners
*
* @param Context ctx
* @param LocationResult result
*/
public void init(Context ctx, LocationResult result){
context = ctx;
locationResult = result;
myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
bestLocation = null;
counts = 0;
// turning on location updates
myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener);
myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);
myLocationManager.addGpsStatusListener(gpsStatusListener);
// starting best location finder loop
handler.postDelayed(showTime, iteration_timeout_step);
}
/**
* GpsStatus listener. OnChainged counts connected satellites count.
*/
public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){
try {
// Check number of satellites in list to determine fix state
GpsStatus status = myLocationManager.getGpsStatus(null);
Iterablesatellites = status.getSatellites();
sat_count = 0;
IteratorsatI = satellites.iterator();
while(satI.hasNext()) {
GpsSatellite satellite = satI.next();
System.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation());
sat_count++;
}
} catch (Exception e) {
e.printStackTrace();
sat_count = min_gps_sat_count + 1;
}
System.println("#### sat_count = " + sat_count);
}
}
};
/**
* Gps location listener.
*/
public final LocationListener gpsLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location){
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
/**
* Network location listener.
*/
public final LocationListener networkLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location){
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
};
/**
* Returns best location using LocationManager.getBestProvider()
*
* @param context
* @return Location|null
*/
public static Location getLocation(Context context){
System.println("getLocation()");
// fetch last known location and update it
try {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
String strLocationProvider = lm.getBestProvider(criteria, true);
System.println("strLocationProvider=" + strLocationProvider);
Location location = lm.getLastKnownLocation(strLocationProvider);
if(location != null){
return location;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}}
min_gps_sat_count如果启用GPS,此课程将尝试连接到卫星。否则返回LocationManager.getBestProvider()位置。检查代码!
android 获取粗略位置_在Android上获取用户当前位置的最简单,最强大的方法是什么?...相关推荐
- android 仿支付宝菜单_这个 Android 几乎放弃的设计,iOS 要用上了?
iOS 和 Android,两个移动时代的操作系统在历经十多年发展后,成熟度之高已经很难再找出大的短板. 如今,这两个系统相似的部分,大概比它们不相似的地方还要多得多.从早期的下拉通知栏,再到现在的侧 ...
- 设置Android studio 3.0显示 光标返回上一次浏览位置 的箭头图标
这几天刚升级了Android studio 3.0,发现光标返回上一次浏览位置的箭头图标没有了.快捷键 ctrl+alt+左右键,按着真不方便.方便按的按键早已经被占满了,也懒得去改动快捷键了.来,把 ...
- android 动画 最顶层_【Android编程实战】StrandHogg漏洞复现及原理分析_Android系统上的维京海盗...
0x00 StrandHogg漏洞详情 StrandHogg漏洞 CVE编号:暂无 [漏洞危害] 近日,Android平台上发现了一个高危漏洞 该漏洞允许攻击者冒充任意合法应用,诱导受害者授予恶意应用 ...
- android 同根动画_[转载]Android anim动画切换效果
关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式: 1. Tween Animation:通过对场景里的对象不断做图像变换(平移.缩放. ...
- android最佳开发实现_在android开发中使用可访问性最佳做法
android最佳开发实现 As you know, accessibility is about building products that everyone can use easily and ...
- android 人脸识别边框_在Android实现人脸识别的详细过程
照相时,在预览画面上提示用户人脸的位置,并完成自动对焦等,是个错的应用; 下面是实现细节 我们知道在android的代码中已有人脸识别的底层算法代码,而且在framework层也封了调用的API函数 ...
- android mvp模式例子_关于Android市场这件事,没有饱和的市场只有饱和的思维
前言 早在几年之前,我们就一直在讨论Android程序员已经饱和的这个问题,直到2020年,Android程序员也没有饱和,相反对高级程序员的需求越来越大. 为什么会有Android程序员已经饱和的错 ...
- android开发java环境_搭建Android开发环境 - Android - mobile - JavaEye论坛
Android的开发现在是如火如荼,逞现在不是很忙了,学习了下,这里记录下了在windows在如何搭建Android开发环境,对自己是个记录,对新入门的兄弟姐妹们可以参考一下! (1)安装JDK,省略 ...
- android实现评论列表_【Android视图效果】分组列表实现吸顶效果
效果图 效果图 分析 先来分析一下,可以看到这是一个按月份分组的2行图片列表,列表顶部一个悬浮栏,会随着列表滑动而刷新,点击顶部栏,弹出了一个筛选框. 思路 1.列表部分 可以用RecyclerVie ...
- android 图片闪光动画_剖析Android动画(图片闪烁、左右摇摆、上下晃动等效果) | 学步园...
图片闪烁,类似这样. 2011-11-22 16:18 上传 左右摇摆: 2011-11-22 17:07 上传 一.续播 (不知道取什么名字好,就是先播放动画A, 接着播放动画B) 有两种方式. ...
最新文章
- 脑机接口需要我们掌握什么样的知识?
- cascader 动态加载 回显_Element中的Cascader(级联列表)动态加载省\市\区数据的方法...
- 图片轮播html实现原理,纯CSS实现图片轮播
- Kobe Bryant crash report
- python爬虫之urllib库详解
- 使用go开启一个能够提供给html的a标签的下载的后端
- hdu2066一个人的旅行(多源点多汇点的最短路径问题)
- 《SpringCloud超级入门》Spring Cloud Eureka是什么?《八》
- .NET生成常用16、32位MD5加密的两种方法
- 提供《疯狂的程序员》下载地址
- 量化交易策略matlab交易方案,【策略分享】Matlab量化交易策略源码分享
- 邓俊辉数据结构学习-7-BST
- R+markdown+LaTeX 中文编译解决方案
- 嵌入式软件架构设计之分层设计
- HTML网页设计制作大作业(div+css)~ 环境保护(5个页面)
- matlab 数字水印攻击 jpeg压缩,基于DFT的数字水印技术研究及模拟.doc
- 高等数学——多元函数极值的定义
- stm32复位引脚NRST
- Java对接微盟开放平台
- C. Interesting Sequence