简体   繁体   English

Android应用程序中的GPS_PROVIDER错误

[英]GPS_PROVIDER Error in Android Application

I have created an Android app to retrieve the location. 我创建了一个Android应用来检索位置。 I tried to use GPS_PROVIDER, it returned null.. But works smooth when i use NETWORK_PROVIDER.. Can any one help me fixing this? 我尝试使用GPS_PROVIDER,它返回null ..但是当我使用NETWORK_PROVIDER时工作顺利..任何人都可以帮我解决这个问题吗?

Here is the code 这是代码

Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 位置location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

The location turns null when i replace "NETWORK_PROVIDER" by "GPS_PROVIDER" .. 当我用"GPS_PROVIDER"替换"NETWORK_PROVIDER"时,该位置变为null。

I have added the following three permissions in manifest 我在清单中添加了以下三个权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

There can be two reasons why your GPS location is null GPS位置为空可能有两个原因

  1. May be your GPS is not active. 可能是你的GPS不活跃。 Try to active your GPS from the setting menu => Location & Security => Check Use GPS Satellites . 尝试从setting menu => Location & Security => Check Use GPS Satellites激活您的GPS setting menu => Location & Security => Check Use GPS Satellites
  2. May be you are currently in the building where the GPS is not working. 可能是您目前在GPS无法正常工作的建筑物中。 Check it by coming out from the building. 从大楼出来检查一下。 This can be happen most of time. 这可能发生在大部分时间。

As per documentation 根据文件

If the provider is currently disabled, null is returned. 如果当前禁用了提供程序,则返回null。

Ensure your GPS is active on the device so you don't receive null. 确保您的GPS在设备上处于活动状态,因此您不会收到空值。

Edit: 编辑:
Also try running requestLocationUpdates() with GPS_PROVIDER or maybe twice, with NETWORK_PROVIDER as well. 还可以尝试使用GPS_PROVIDER运行requestLocationUpdates() ,也可以使用NETWORK_PROVIDER运行两次。 The LocationManager retrieves the location the fastest way and it stops searching. LocationManager以最快的方式检索位置并停止搜索。 Since GPS location is retrieved slower than the network location, is stops right after if finds it therefore returning null for the GPS. 由于GPS位置的检索速度比网络位置慢,因此如果发现GPS位置因此返回null,则会立即停止。

The location turns null when i replace "NETWORK_PROVIDER" by "GPS_PROVIDER" 当我用“GPS_PROVIDER”替换“NETWORK_PROVIDER”时,该位置变为null

To solve this issue be sure to have enabled the Location services : 要解决此问题,请务必启用位置服务

在此输入图像描述

remember the docs: 记住文档:

If the provider is currently disabled, null is returned. 如果当前禁用了提供程序,则返回null。

As an option you can use both providers if GPS is disabled then check network provider: 如果禁用GPS,您可以使用两个提供商,然后检查网络提供商:

...
 myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 if(myLocation == null) {
            myLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 }

    if(myLocation != null) {
         //Get values for latitude and longitude!
    }
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android onLocationUpdate未使用GPS_PROVIDER调用 - Android onLocationUpdate not called with GPS_PROVIDER GPS_PROVIDER的位置 - Location with GPS_PROVIDER 如果设置了APN,Android GPS_PROVIDER是否使用A-GPS? - Does android GPS_PROVIDER use A-GPS if APN is set? 来自Android中GPS_PROVIDER或NETWORK_PROVIDER的位置不一致 - Location from GPS_PROVIDER or NETWORK_PROVIDER in android is not consistent 在android中,GPS_PROVIDER有效,但NETWORK_PROVIDER不起作用 - in android, GPS_PROVIDER works, but NETWORK_PROVIDER does not Android:locationManager GPS_provider VS Network_provider - Android: locationManager GPS_provider VS Network_provider 反正有使用Android中的GPS_PROVIDER快速请求LocationUpdates - Is there anyway to requestLocationUpdates fast using GPS_PROVIDER in android Android requestLocationUpdates 不起作用(GPS_PROVIDER、ACCESS_FINE_LOCATION) - Android requestLocationUpdates not working (GPS_PROVIDER, ACCESS_FINE_LOCATION) 如何在Android中使用GPS_PROVIDER获取当前准确的GPS坐标? - How to get the current accurate GPS Coordinates using GPS_PROVIDER in android? Android LocationManager从NETWORK_PROVIDER或GPS_PROVIDER获取经度和纬度是否更加耗费资源? - Android LocationManager is it more resource intensive to get the longitude and latitude from NETWORK_PROVIDER or GPS_PROVIDER?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM