gpt4 book ai didi

java - 网络提供商与 GPS 提供商模拟位置 android

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:11 25 4
gpt4 key购买 nike

在我的应用程序中想要模拟我的位置到给定的地方。目前,它会调用两个提供商并将位置欺骗到我提供的位置。正如您在 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 提供商收到的位置。

关于java - 网络提供商与 GPS 提供商模拟位置 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com