andriod 自动切换网络和gps定位
来源:互联网 发布:超级高铁 知乎 编辑:程序博客网 时间:2024/05/01 03:11
获取到位置服务以后,同时请求网络和gps定位更新,然后就会同时上报网络和gps的Location 信息。在没有gps信号的时候,会自动获取网络定位的位置信息,如果有gps信号,则优先获取gps提供的位置信息.isBetterLocation 根据 时间、准确性、定位方式等判断是否更新当前位置信息,该方法来源于开发指南的Obtaining User Location 下。
package cncit.gps;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class UploadgpsActivity extends Activity
{
LocationManager lm = null;
Location myLocation = null;
TextView loc, timeText;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loc = (TextView) findViewById(R.id.loc);
timeText = (TextView) findViewById(R.id.time);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onResume()
{
super.onResume();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
listener);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
listener);
Log.e("onResume", "onResume");
}
@Override
protected void onPause()
{
super.onPause();
Log.e("onPause", "onPause");
lm.removeUpdates(listener);
}
LocationListener listener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
// 实际上报时间
//String time = sdf.format(new Date(location.getTime()));
//timeText.setText("实际上报时间:" + time);
if (isBetterLocation(location, myLocation))
{
//获取纬度
double lat = location.getLatitude();
//获取经度
double lon = location.getLongitude();
//位置提供者
String provider = location.getProvider();
//位置的准确性
float accuracy = location.getAccuracy();
//高度信息
double altitude = location.getAltitude();
//方向角
float bearing = location.getBearing();
//速度 米/秒
float speed = location.getSpeed();
String locationTime = sdf.format(new Date(location.getTime()));
String currentTime = null;
if (myLocation != null)
{
currentTime = sdf.format(new Date(myLocation.getTime()));
myLocation =location;
}
else
{
myLocation =location;
}
loc.setText("经度:" + lon
+ "\n纬度:" + lat
+ "\n服务商:"+ provider
+ "\n准确性:"+ accuracy
+ "\n高度:"+ altitude
+ "\n方向角:"+ bearing
+ "\n速度:"+ speed
+ "\n上次上报时间:"+currentTime
+ "\n最新上报时间:"+locationTime);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e("onStatusChanged", "onStatusChanged: " + provider);
}
@Override
public void onProviderEnabled(String provider)
{
Log.e("onProviderEnabled", "onProviderEnabled: " + provider);
}
@Override
public void onProviderDisabled(String provider)
{
Log.e("onProviderDisabled", "onProviderDisabled: " + provider);
}
};
private static final int TWO_MINUTES = 1000 * 1 * 2;
/**
* Determines whether one Location reading is better than the current
* Location fix
*
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new
* one
*/
protected boolean isBetterLocation(Location location,
Location currentBestLocation)
{
if (currentBestLocation == null)
{
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use
// the new location
// because the user has likely moved
if (isSignificantlyNewer)
{
return true;
// If the new location is more than two minutes older, it must be
// worse
}
else if (isSignificantlyOlder)
{
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate)
{
return true;
}
else if (isNewer && !isLessAccurate)
{
return true;
}
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider)
{
return true;
}
return false;
}
/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2)
{
if (provider1 == null)
{
return provider2 == null;
}
return provider1.equals(provider2);
}
}
- andriod 自动切换网络和gps定位
- Android Gps定位和网络定位
- Android 获取GPS和网络定位信息
- 网络定位、A-GPS和GPS的关系
- Android-GPS,网络定位
- android network,gps定位切换
- android中基于网络和GPS的不同精度定位
- Android获取当前位置(GPS和网络定位)
- Android学习之使用手机网络和GPS定位
- Android获取当前位置(GPS和网络定位)
- GPS自动定位,城市选择器
- Android网络定位或GPS定位
- Android GPS和Network定位
- GPS简介和定位过程
- 离线定位和GPS定位的区别
- LBS定位和GPS定位的区别
- 3G网络的SUPLA-GPS定位技术
- android网络-GPS获取定位信息
- 探索ORACLE不完全恢复之--基于检查点恢复 第二篇
- POJ 2318 计算几何 叉积水题
- VC中使程序最小化到系统托盘区
- USB接口定义 颜色
- Wince600 关于RDP连接问题
- andriod 自动切换网络和gps定位
- Android Adapter(适配器)优化
- Visual Studio 目标框架造成 命名空间“Microsoft”中不存在类型或命名空间名称“Crm”。是否缺少程序集引用中错误的处理
- MVC3 checkbox 取值
- 1120. Sum of Sequential Numbers
- javascript中toFixed不同浏览器解析不一样的问题,好扯。。。。
- 探索ORACLE不完全恢复之--基于cancel恢复 第一篇
- lanmp.sh安装文件修改
- 用flash制作水印的一个方法