1

我想从 GPS 接收更新,但我不能。方法 onLocationChanged 没有调用。在这个应用程序中,我也使用 Google Maps API。我不知道,也许这是我的问题的一些原因。

MainActivity 中关于 GPS 的代码:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) 
            {
                      txtLat.setText("Latitude: " + String.valueOf((int)(location.getLatitude()))  + ","
                              + String.valueOf(((location.getLatitude() * 100) % 100)) + " degrees");

                    txtLong.setText("Longtitude: " + String.valueOf((int)(location.getLongitude()))  + ","
                              + String.valueOf(((location.getLongitude() * 100) % 100)) + " degrees");

            }
public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
          };

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

在清单中我已经声明了这些权限:

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

以及 Google Maps API v2 所需的其他内容

编辑:LogCat:

06-27 14:25:28.785: D/dalvikvm(5206): GC_FOR_ALLOC freed 42K, 8% free 2500K/2716K, paused 42ms, total 48ms
06-27 14:25:28.836: I/dalvikvm-heap(5206): Grow heap (frag case) to 6.147MB for 3712016-byte allocation
06-27 14:25:28.906: D/dalvikvm(5206): GC_FOR_ALLOC freed 2K, 4% free 6122K/6344K, paused 64ms, total 64ms
06-27 14:25:28.965: D/dalvikvm(5206): GC_CONCURRENT freed <1K, 4% free 6122K/6344K, paused 8ms+5ms, total 66ms
06-27 14:25:29.595: D/dalvikvm(5206): GC_CONCURRENT freed 3766K, 53% free 3523K/7464K, paused 24ms+46ms, total 149ms
06-27 14:25:30.025: D/dalvikvm(5206): GC_CONCURRENT freed 243K, 51% free 3681K/7464K, paused 5ms+5ms, total 52ms
06-27 14:25:30.045: E/Google Maps Android API(5206): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-27 14:25:30.385: D/dalvikvm(5206): GC_FOR_ALLOC freed 146K, 49% free 3826K/7464K, paused 39ms, total 40ms
06-27 14:25:30.685: D/dalvikvm(5206): GC_CONCURRENT freed 82K, 44% free 4234K/7464K, paused 16ms+26ms, total 133ms
06-27 14:25:31.066: D/libEGL(5206): Emulator without GPU support detected. Fallback to software renderer.
06-27 14:25:31.095: I/Choreographer(5206): Skipped 77 frames!  The application may be doing too much work on its main thread.
06-27 14:25:31.115: D/libEGL(5206): loaded /system/lib/egl/libGLES_android.so
06-27 14:25:31.175: D/gralloc_goldfish(5206): Emulator without GPU emulation detected.
06-27 14:25:31.819: D/dalvikvm(5206): GC_CONCURRENT freed 402K, 41% free 4428K/7464K, paused 30ms+60ms, total 386ms
06-27 14:25:31.915: D/dalvikvm(5206): WAIT_FOR_CONCURRENT_GC blocked 428ms
06-27 14:25:32.427: D/dalvikvm(5206): GC_CONCURRENT freed 630K, 41% free 4429K/7464K, paused 31ms+18ms, total 100ms
06-27 14:25:32.427: D/dalvikvm(5206): WAIT_FOR_CONCURRENT_GC blocked 59ms
06-27 14:33:03.695: E/Trace(5381): error opening trace file: No such file or directory (2)
06-27 14:33:04.395: D/dalvikvm(5381): GC_FOR_ALLOC freed 46K, 9% free 2500K/2720K, paused 42ms, total 45ms
06-27 14:33:04.435: I/dalvikvm-heap(5381): Grow heap (frag case) to 6.147MB for 3712016-byte allocation
06-27 14:33:04.485: D/dalvikvm(5381): GC_FOR_ALLOC freed 2K, 4% free 6122K/6348K, paused 43ms, total 44ms
06-27 14:33:04.555: D/dalvikvm(5381): GC_CONCURRENT freed <1K, 4% free 6122K/6348K, paused 5ms+5ms, total 70ms
06-27 14:33:05.235: D/dalvikvm(5381): GC_CONCURRENT freed 3774K, 53% free 3500K/7448K, paused 20ms+84ms, total 157ms
06-27 14:33:05.615: D/dalvikvm(5381): GC_CONCURRENT freed 232K, 51% free 3663K/7448K, paused 72ms+5ms, total 118ms
06-27 14:33:05.645: E/Google Maps Android API(5381): Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
06-27 14:33:05.875: D/dalvikvm(5381): GC_FOR_ALLOC freed 190K, 49% free 3847K/7448K, paused 39ms, total 40ms
06-27 14:33:06.268: D/dalvikvm(5381): GC_CONCURRENT freed 62K, 44% free 4223K/7448K, paused 71ms+77ms, total 284ms
06-27 14:33:06.815: D/libEGL(5381): Emulator without GPU support detected. Fallback to software renderer.
06-27 14:33:06.865: D/libEGL(5381): loaded /system/lib/egl/libGLES_android.so
06-27 14:33:06.906: I/Choreographer(5381): Skipped 120 frames!  The application may be doing too much work on its main thread.
06-27 14:33:07.005: D/gralloc_goldfish(5381): Emulator without GPU emulation detected.
06-27 14:33:07.455: I/Choreographer(5381): Skipped 38 frames!  The application may be doing too much work on its main thread.
06-27 14:33:07.545: D/dalvikvm(5381): GC_CONCURRENT freed 381K, 41% free 4427K/7448K, paused 8ms+111ms, total 349ms
06-27 14:33:07.545: D/dalvikvm(5381): WAIT_FOR_CONCURRENT_GC blocked 237ms
06-27 14:33:08.246: D/dalvikvm(5381): GC_CONCURRENT freed 630K, 41% free 4430K/7448K, paused 72ms+7ms, total 171ms
06-27 14:33:08.246: D/dalvikvm(5381): WAIT_FOR_CONCURRENT_GC blocked 32ms
4

1 回答 1

1

如果您尝试使用 GPS 提供程序检索位置,并且您在建筑物内或 GPS 接收不佳,则永远不会调用 onLocationChanged。

不会自动回退到 Wifi 或蜂窝塔。

始终检查您使用的是哪种提供程序,即使在指定标准时也是如此。

Google 通过 Google Play 服务发布了一系列新的位置 API,它们提供了更有效的检索用户位置的方法(提高准确性和电池寿命)。

我建议您看一下这些并按照检索当前位置教程(它包含一个您可以下载的示例应用程序)。

于 2013-06-27T15:21:55.773 回答