/**
* 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();
* }
* };
*/
public class MyLocations{
/**
* 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.out.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.out.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.out.println("Connected " + sat_count + " sattelites. continue waiting..");
handler.postDelayed(this, iteration_timeout_step);
}else{
System.out.println("#########################################");
System.out.println("BestLocation finded return result to main. sat_count=" + sat_count);
System.out.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 < min_gps_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);
Iterable<GpsSatellite>satellites = status.getSatellites();
sat_count = 0;
Iterator<GpsSatellite>satI = satellites.iterator();
while(satI.hasNext()) {
GpsSatellite satellite = satI.next();
System.out.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation());
sat_count++;
}
} catch (Exception e) {
e.printStackTrace();
sat_count = min_gps_sat_count + 1;
}
System.out.println("#### sat_count = " + sat_count);
}
}
};
/**
* Gps location listener.
*/
public final LocationListener gpsLocationListener = new LocationListener(){
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(){
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.out.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.out.println("strLocationProvider=" + strLocationProvider);
Location location = lm.getLastKnownLocation(strLocationProvider);
if(location != null){
return location;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
分享到:
相关推荐
无需安装,即点即用,放在任何位置运行即可完成封装。 软件提供了丰富的封装及部署选项,并留有多组外部接口。 不用更改系统任何设置,Newprep可轻松完成windows封装。 您可以什么都不懂、什么都不会,Newprep为您...
右键要转移的项目属性——位置 X:\Users\Administrator 即可 1.C:\Windows\Web\Wall*** (Windows自带墙纸)推荐转移 2.C:\Windows\System32\DriverStore\FileRepository 下 搜索输入 ati*.inf (14.6M) nv*...
再次重申报毒问题,封装软件肯定涉及系统深层的东西,还会修改部分系统文件与系统服务,被误报纯属正常(被某些国产杀毒误报就更正常不过了),微软官方杀毒没把ES3中任何一个文件当做病毒,大家可以自己尝试。...
geoserver接口文档,geoserver二次开发,封装地图gis相关操作。 OGC 服务是 OGC(Open Geospatial Consortium,开放地理信息联盟),制定的一系列 GIS 服务规范。本文档主要介 绍 K-TopGeoserver Server 提供的 WMS...
2、[更新]重写预置网络位置代码确保成功率; 3、[更新]通过购买渠道获得的SC特权服务不再集成软件魔盒,需找客服开通; 4、[修复]在部分环境跳过OOBE未能自动激活administrator用户(反馈者:信息-郗广宗); 5、[修复]...
Easy Sysprep系统封装工可自由定义封装项目: 封装OEM信息:序列号、证书、注册组织、注册用户、系统时区及语言、界面语言及用户习惯、网络位置及安全更新、工组组、域及域用户等参数。 封装选项: 将当前用户...
(5)支持直接将7Z转化为EXE文件,并配以漂亮的解压界面,且可以根据您的设定完全自动化解压到某位置。 13、完成 (1)ES3提供将您的操作习惯保存起来的功能。 (2)由于ES3的配置文件比较复杂,为了让用户修改...
YxdIocp 是 Delphi Windows IOCP 通讯模型封装,基于DIOCP。YxdIocp包含有支持大并发的TCP服务组件、HTTP服务组件、UDP服务组件、WebSocket服务组件,和TCP、UDP等基础客户端组件。Demo 源码需要重新设置下搜索路径...
HMS Pushkit Csharp Severdemo English | 目录 ... 将解压缩的AGConnectAdmin项目复制到应用程序的Visual Studio解决方案中的适当位置,然后在应用程序项目中引用该项目。 请参考示例代码,以了解如
第一步:进入腾讯位置服务 第二步:点击接入指引 根据上图进行登录,然后验证手机和邮箱,再前往申请秘钥(key) 以上步骤完成后: 进入key的申请页面: 重点提示:小程序不需要在WebServiceAPI填写域名,...
源码LocationManager,代码库原先有一LocationManager,这个和...这个是用Swift封装的CLLocationManager,提供地理位置更新、地理编码、逆向地理编码(reverse geocoing),使用的是苹果和谷歌的服务。 支持iOS 7 & 8
简单的Ruby用例/交互器/服务对象来封装业务逻辑。 有关详细信息,请参见此。 那是什么 以上三个术语在不同时间都用于描述与交付机制(读取ApplicationController)分开的专用对象的使用,以协调对多个域对象(例如...
从执行体程序库中的CLLogger类...该类要提供数据缓存服务。 调用该类的写操作接口时,数据要首先写到缓存,然后再根据策略写到文件中。 调用该类的读操作接口时,该类能根据策略缓存读出的数据 读写缓存要保持一致性
每当有客户端的游戏位置发生改变的时候就向从服务器发送改变信息,从服务器保存、修改所有与它连接的客户端的位置信息列表。 当一张桌子坐满时游戏开始。游戏开始后客户端也是和从服务器通信以更新游戏信息。 当...
本文介绍了浅谈在Vue-cli里基于axios封装复用请求,分享给大家,具体如下: 安装 只用安装一个axios就可以了。 npm install axios --save 接口代理设置 为了请求可以正常发送,我们一般要进行一个接口代理的配置,...
软件开发设计:PHP、QT、应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、...
2.0服务获取token的URL,请参考。 push_open_url 访问HUAWEI Push Kit的网址,请参考。 示例代码 1. 发送安卓数据消息。 代码位置:examples/sendDataMessage.java 2. 发送 Android 通知消息。 代码位置:examples/...
面向对象:Java全面支持面向对象的特性,如封装、继承和多态,使得代码更易于维护和扩展。 安全:Java提供了丰富的安全特性,如禁止指针运算、自动内存管理和异常处理机制,以减少程序错误和恶意攻击的可能性。 ...
用vc++6.0开发的http post 应用,上传本地文件到服务器,服务采用asp.net接收,存在服务器指定位置。用vc++6,0封装了CHttp类,有完整的两个实例,亲测可用。