android如何为手机设置虚假位置

标签 android location

我尝试对手机使用模拟位置,但它显示异常,

我的代码在下面,

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/

相关文章:

android - Flutter mask 一个圆圈变成一个容器

android - 如何获得 ListView 的焦点子项?

iphone - didEnterRegion 当应用程序未运行时

android - 在 Android 中使用纬度和经度值制作 Location 对象

iphone - Xcode中使用GPX模拟位置变化时,有没有办法控制速度?

java - Realm :通过 id 创建关系

android - 如何使用改造解析动态键响应

android - 启动时的位置更新 - 在 Activity 中打开和关闭

ios - 为什么GeoFire的函数getLocationForKey会被超越?

android - 如何从 Android 中的 Chrome 选项卡获取回调,例如 web View 提供了 shouldOverrideUrlLoading、onLoadResource、onPageFinished