我尝试对手机使用模拟位置,但它显示异常,
我的代码在下面,
Location location = new Location(MOCK_PROVIDER_NAME);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setTime(new Date().getTime());
locationManager.setTestProviderEnabled(MOCK_PROVIDER_NAME, true);
locationManager.setTestProviderLocation(MOCK_PROVIDER_NAME, location);
Toast.makeText(getBaseContext(),"Latitude: "+location.getLatitude()+" Longitude: "+location.getLongitude(), Toast.LENGTH_SHORT)
.show();
我给了 MOCK_PROVIDER_NAME= "gps"但这个没有用,我也尝试了 "network"和 "spoof",但它不起作用。
我哪里出错了,请有人告诉我。 谢谢
最佳答案
以下是导致问题的两个可能原因:
安全异常:
if the ACCESS_MOCK_LOCATION permission is not present or the Settings.Secure.ALLOW_MOCK_LOCATION system setting is not enabled
或者 IllegalArgumentException:
if a provider with the given name already exists
我从以下引述:http://developer.android.com/reference/android/location/LocationManager.html#addTestProvider
如果它不是您遇到的异常之一,请向我们提供您的错误的堆栈跟踪以及堆栈跟踪在您的代码中显示的任何行是问题所在。
我猜你没有在 list 中声明权限和/或在开发设置中关闭了“允许模拟位置”。
关于android如何为手机设置虚假位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7291937/