当前位置:操作系统 > 安卓/Android >>

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);
}


}


作者:qq1761310972
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,