Android模拟gps提供商
我的代码片段是:Android模拟gps提供商,android,gps,android-location,Android,Gps,Android Location,我的代码片段是: mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if(mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER); } mLocationMana
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
}
mLocationManager.addTestProvider(LocationManager.GPS_PROVIDER,
"requiresNetwork" == "", "requiresSatellite" == "",
"requiresCell" == "", "hasMonetaryCost" == "",
"supportsAltitude" == "", "supportsSpeed" == "",
"supportsBearing" == "",
android.location.Criteria.POWER_LOW,
android.location.Criteria.ACCURACY_FINE);
location = new Location(LocationManager.GPS_PROVIDER);
mLocationManager.setTestProviderEnabled(
LocationManager.GPS_PROVIDER, true);
// mLocationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER,
// location);
location.setLatitude(FinalLatitudeIntArray[0]);
location.setLongitude(FinalLongitudeIntArray[0]);
mLocationManager.setTestProviderLocation(
LocationManager.GPS_PROVIDER, location);
我得到了一个错误:
java.lang.IllegalArgumentException:提供程序“gps”未知
在android.os.Parcel.readException(Parcel.java:1326)
位于android.os.Parcel.readException(Parcel.java:1276)
在android.location.ILocationManager$Stub$Proxy.removeTestProviderEnabled(ILocationManager.java:1097)
在android.location.LocationManager.removeTestProviderEnabled(LocationManager.java:1130)中
请帮我整理一下这个错误。
加上。
早些时候,应用程序运行正常,但当我重新启动系统时,应用程序开始显示错误。来自location manager上的Android文档:
removeTestProvider() throws
IllegalArgumentException if no provider with the given name exists
因此,如果您在emulator上进行测试,GPS的设置可能已在重新启动时重置(尝试检查您的权限和DDMS以再次启用它)。如果在设备上,则必须禁用GPS(转到设置并启用GPS)
编辑:找到相关内容:。基本上,模拟器中出现了一些不稳定的情况。从该线程上的注释中,尝试使用标准.accurity\u FINE
而不是LocationManager.GPS\u PROVIDER
,例如:
LocationManager locationManager = (LocationManager)context.getSystemService( Context.LOCATION_SERVICE );
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );
String provider = locationManager.getBestProvider( criteria, true );
if ( provider == null ) {
Log.e( TAG, "No location provider found!" );
return;
}
通过该线程了解更多信息。我也遇到了同样的问题,因为现在有些设备没有GPS。“varevarao”提到了一个很好的方法,但如果使用criteria方法,我也会遇到问题。在某些设备上,它仍然在“getBestProvider”处返回空值(请参阅下面的代码) 我这样做是为了解决这个问题(参见下面的代码)。不确定这是否是最好的方法,但它目前对我有效。它的工作原理是获取设备上可用的“位置提供者”列表,然后获取第一个(应该没关系,因为我只是将其用于模拟位置)
List tmpProviders=locationManager.getAllProviders();
如果(!tmpProviders.isEmpty()&&tmpProviders.toArray()[0]!=null&&tmpProviders.toArray()[0].toString().isEmpty()){
moLocationProvider=tmpProviders.toArray()[0].toString();
}否则{//您的错误会出现在这里}
@rekire感谢您编辑此问题。你能帮我找到一个合适的解决方案吗?没问题,所以读起来更简单。您的设备支持GPS吗?不,我提供自己的lat和long值。昨天代码运行正常,但今天我遇到了以下错误:(..您可以添加stacktrace的更多行吗?该跟踪似乎与您的代码没有直接关系。当我删除removeTestProvider()行时,会对SettProvider行引发相同的异常:(..当我删除removeTestProvider()行时,会对SettProvider行引发相同的异常..):(……那么几乎可以肯定的是,您的设备/模拟器上没有启用GPS提供程序。它必须具备该功能,才能接收模拟位置。我根本没有更改模拟器设置。那么,为什么它在重新启动之前工作得很好?????应用程序现在运行平稳,使用了我以前使用的代码,没有showing任何错误。那是什么?正如我在回答中提到的,通过以下链接:
Criteria criteria = new Criteria();
criteria.setAccuracy( Criteria.ACCURACY_FINE );
String provider = locationManager.getBestProvider( criteria, false );
if ( provider == null ) {
criteria.setAccuracy( Criteria.ACCURACY_COARSE );
provider = locationManager.getBestProvider( criteria, true );
Log.d( "Function", "No location provider found!" );
return;
}
List<String> tmpProviders = locationManager.getAllProviders();
if (!tmpProviders.isEmpty() && tmpProviders.toArray()[0] != null && !tmpProviders.toArray()[0].toString().isEmpty() ) {
mocLocationProvider = tmpProviders.toArray()[0].toString();
} else {// your error goes here }