我看到,在市场上有很多模拟位置应用程序适用于整个系统。 (这意味着当我们使用谷歌地图,超级...)它会显示新的位置。如何写模拟GPS提供商影响整个系统应用程序
我跟着一些教程。这里是我的示例代码:
public class MainActivity extends AppCompatActivity {
LocationManager mLocationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
setupMockLocations(LocationManager.GPS_PROVIDER);
setupMockLocations(LocationManager.NETWORK_PROVIDER);
setupMockLocations(LocationManager.PASSIVE_PROVIDER);
}
private void setupMockLocations(final String providerName) {
new Thread(new Runnable() {
@Override
public void run() {
for (; ;) {
mLocationManager.addTestProvider(providerName, false, //requiresNetwork,
false, // requiresSatellite,
false, // requiresCell,
false, // hasMonetaryCost,
false, // supportsAltitude,
false, // supportsSpeed, s
false, // upportsBearing,
Criteria.POWER_LOW, // powerRequirement
Criteria.ACCURACY_FINE); // accuracy
mLocationManager.setTestProviderEnabled(providerName, true);
location.setTime(System.currentTimeMillis());
location.setAccuracy(1);
location.setTime(System.currentTimeMillis());
Method locationJellyBeanFixMethod = null;
try {
locationJellyBeanFixMethod = Location.class.getMethod("makeComplete");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (locationJellyBeanFixMethod != null) {
try {
locationJellyBeanFixMethod.invoke(location);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
mLocationManager.setTestProviderLocation(providerName, location);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.e("print", "mocking " + providerName);
}
}
}).run();
}
}
但看起来像这样的代码不起作用。 (它应该在Google地图上显示我的sampleLocation
而不是当前位置)。我也伪造GPS_PROVIDER
NETWORK_PROVIDER
和PASSIVE_PROVIDER
。请告诉我如何做到这一点。
我改变了我的代码。我定期调用'setupMockLocation',但它仍然不起作用。也许还有其他错误? –
当你离开你的应用程序时发生了什么?您应该在服务中运行相关代码,因为在切换到Google地图(或其他任何地方)后,您的活动(和模拟位置)将停止,然后Google地图会收到新的位置信息。 – Blackkara
当我离开我的应用程序时,线程仍定期运行模拟位置(我已通过打印日志进行检查)。 –