GPS不工作在我的应用程序,但在谷歌地图工作



我对三星Galaxy Tab (GT-P7310)上的GPS组件有一个奇怪的问题:GPS在谷歌地图中工作正常,但在我自己的应用程序中不提供任何位置。虽然我的应用程序在我的三星Galaxy S2上运行良好。

我正在像这样调用location-service:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location currentLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
// Constantly update the location
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), false), 0, 0, listener);

,但我得到的是null作为位置,回调监听器从未被调用。

平板电脑运行Android 4.2.2与CyanogenMod 10.1-20130512-UNOFFICIAL-p5wifi。打开位置访问(否则谷歌地图也不能工作)。

我的应用程序在其Manifest中设置了以下权限:

  • ACCESS_FINE_LOCATION
  • ACCESS_COARSE_LOCATION
  • 互联网

有任何想法,为什么我没有得到这个设备上的位置?

解决方案:您可以使用GPS,而不是通过决定标准来选择最佳提供商。

例子:

代替

    Location currentLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
// Constantly update the location
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), false), 0, 0, listener);

    Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Constantly update the location
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

解释:

根据应用程序的用例,您可以选择特定的位置提供者即LocationManager.GPS_PROVIDERLocationManager.NETWORK_PROVIDER

或者,你可以提供一些输入标准,如准确性,功率要求,货币成本等,让Android决定最接近匹配的位置提供商

    // Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
String providerName = locManager.getBestProvider(criteria, true);
//and then you can make location update request with selected best provider
locationManager.requestLocationUpdates(providerName, 0, 0, listener); 

看一下如何使用locationmanager,如何指定Criteria以及getBestProvider方法如何为引用工作

我希望它会有所帮助!!

最新更新