andriod 自动切换网络和gps定位


获取到位置服务以后,同时请求网络和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);
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值