在我的应用中想要将我的位置模拟到指定位置。现在,它会调用两个提供商并将位置欺骗到我给它的位置。如你在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;
}
这两种方式都是一种好方法吗?
答案 0 :(得分:0)
网络提供商意味着该位置基于手机信号塔和WiFi接入点的可用性。 GPS提供商的位置基于卫星,这个基本上具有更好的准确性。 几乎所有应用程序都优先考虑GPS提供商收到的职位。