[英]Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent
[英]Network Provider vs Gps provider to mock location android
在我的应用中要模拟我的位置到给定的位置。 现在,它既称为提供者,又将位置欺骗到我给它的位置。 就像在run函数中看到的那样,它每秒调用一次。 网络提供商和Gps提供商之间的区别是什么? 我该怎么用? 现在,我同时使用它们,这是我的代码:
public boolean
startMockLocation(com.quinny898.library.persistentsearch.Location location) {
if (location != null && location.getLng() == -1000 && location.getLat() == -1000) {
try {
PlaceAPI placeApi = new PlaceAPI();
placeApi.getLatLng(location);
} catch (Exception e) {
}
}
if (location != null)
if (location.getLng() != -1000 && location.getLat() != -1000) {
this.currentLocation = location;
try {
if(!hasProvider) {
LocationManager lm = (LocationManager) mContext.getSystemService(
Context.LOCATION_SERVICE);
// Toast.makeText(mContext, "Lidt: " + lm.getAllProviders(), Toast.LENGTH_SHORT).show();
lm.addTestProvider(LocationManager.NETWORK_PROVIDER, false, false, false, false, false,
true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE);
lm.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);
LocationManager lm2 = (LocationManager) mContext.getSystemService(
Context.LOCATION_SERVICE);
lm2.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false,
true, true, android.location.Criteria.POWER_LOW, android.location.Criteria.ACCURACY_FINE);
lm2.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
this.hasProvider = true;
}
isWorking = setMockLocation(location);
setMockLocation2(location);
}catch (Exception e)
{
isWorking = false;
}
return isWorking;
}
return false;
}
public void run() {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
try {
if (isWorking) {
if (currentLocation != null) {
setMockLocation(currentLocation);
setMockLocation2(currentLocation);
}
}
sleep(1000);
} catch (InterruptedException e) {
// e.printStackTrace();
Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
};
thread.start();
}
/*
Stop the spoofed location and return to the source location
*/
public void stopMockLocation() {
if (isWorking) {
LocationManager lm = (LocationManager) mContext.getSystemService(
Context.LOCATION_SERVICE);
lm.removeTestProvider(LocationManager.NETWORK_PROVIDER);
lm.removeTestProvider(LocationManager.GPS_PROVIDER);
currentLocation = null;
this.isWorking = false;
}
}
private boolean setMockLocation(com.quinny898.library.persistentsearch.Location location) {
try
{
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
android.location.Location newLocation = new android.location.Location(LocationManager.NETWORK_PROVIDER);
newLocation.setLatitude(location.getLat());
newLocation.setLongitude(location.getLng());
newLocation.setAccuracy(500);
newLocation.setTime(System.currentTimeMillis());
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
lm.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, newLocation);
} catch (Exception e ) {
hasProvider = false;
return false;
}
return true;
}
private boolean setMockLocation2(com.quinny898.library.persistentsearch.Location location) {
try {
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
android.location.Location newLocation = new android.location.Location(LocationManager.GPS_PROVIDER);
newLocation.setLatitude(location.getLat());
newLocation.setLongitude(location.getLng());
newLocation.setAccuracy(500);
newLocation.setTime(System.currentTimeMillis());
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
} // todo
lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, newLocation);
} catch (Exception e) {
hasProvider = false;
return false;
}
return true;
}
称这两者为好方法吗?
网络提供商表示位置取决于蜂窝塔和WiFi接入点的可用性。 GPS提供商的位置是基于卫星的,这一精度基本上更好。 几乎所有应用程序都将GPS提供商接收到的位置的优先级更高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.