为什么我的NETWORK_PROVIDER突然停止调用onLocationChanged?

时间:2013-03-08 14:45:16

标签: android networking gps locationmanager google-maps-android-api-2

我的GPS应用程序中的NETWORK_PROVIDER突然停止了工作。它与我的互联网连接无关,因为我手机上的其他所有应用程序都可以与互联网配合使用。我用GPS_PROVIDER尝试了它并且它完全正常,并且在调用onLocationChanged之后它能够从互联网上检索位置,因此这进一步恢复了互联网的工作。

我的手机:

  • 三星Galaxy S2
  • Android版本4.0.3

我试过的事情无济于事:

  • 清理项目
  • 在不同地点进行外部测试。同样,GPS工作,但网络提供商没有。
  • 重新启动手机/关闭手机
  • 在手机上卸载应用,然后重新安装
  • 开启和关闭互联网

我再说一遍,一切都很好,然后突然间它无缘无故地停止工作。在此过渡期间,我根本没有改变我的代码。

然而,这是我的代码的一部分,这里是在pastebin http://pastebin.com/itrXgWr7

首先调用它,它可以输入if(networkEnabled)然后在LogCat中打印出"Getting updates from network provider"

networkEnabled = providerHandler.isNetworkEnabled();
GPSEnabled = providerHandler.isGPSEnabled();

if(!GPSEnabled && !networkEnabled)
{
    Toast.makeText(this, "Error: This application requires a GPS or network connection",
            Toast.LENGTH_SHORT).show();
}
else
{
    if(GPSEnabled)
    {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
    else if(networkEnabled)
    {
        System.out.println("Getting updates from network provider");
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    }
}

这是LocationListener。永远不会使用NETWORK_PROVIDER调用onLocationChanged。我再说一遍,它用来调用它,但不再这样了。当我关闭GPS时会发生什么事情就是直接调用onProviderDisabled,给我一个"Disabled provider network"的祝酒消息。这是Wifi和移动互联网开启:

    //Get Latitude and Longitude of current location and use that to get the surrounding markers. 
@Override
public void onLocationChanged(Location location)
{
    //Get coordinates
    double lat = (location.getLatitude());
    double lng = (location.getLongitude());
    Log.d("MainActivity", "got location: " + lat + ": " + lng);
    //get nearest locations
    new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng); 

    // Zoom in, animating the camera after the markers have been placed
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
    System.out.println("lat = " + lat + ", lng = " + lng);

     //Stop listening for updates. We only want to do this once. 
     locManager.removeUpdates(this);
}


public void onProviderDisabled(String provider)
{
  Toast.makeText(MainActivity.this, "Disabled provider " + provider,
        Toast.LENGTH_SHORT).show();
}

public void onProviderEnabled(String provider)
{
      Toast.makeText(MainActivity.this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
}

public void onStatusChanged(String provider, int status,
                              Bundle extras)
{
  // required for interface, not used
}

我在XML文件中拥有所有必要的权限,如您所见:

<uses-permission android:name="com.example.neatspots.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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_NETWORK_STATE"/>

这是我的LogCat:

03-08 14:27:58.663: D/dalvikvm(4176): GC_CONCURRENT freed 223K, 3% free 9072K/9351K, paused 2ms+2ms
03-08 14:27:58.823: D/dalvikvm(4176): GC_CONCURRENT freed 272K, 4% free 9239K/9607K, paused 2ms+2ms
03-08 14:27:58.948: D/dalvikvm(4176): GC_CONCURRENT freed 263K, 4% free 9545K/9863K, paused 2ms+3ms
03-08 14:27:59.018: D/dalvikvm(4176): GC_FOR_ALLOC freed 76K, 3% free 9742K/9991K, paused 29ms
03-08 14:27:59.048: I/System.out(4176): GPS: false
03-08 14:27:59.048: I/System.out(4176): Network: true
03-08 14:27:59.048: I/System.out(4176): Getting updates from network provider
03-08 14:27:59.063: D/SensorManager(4176): ====>>>>>Num Sensor: 1
03-08 14:27:59.063: D/SensorManager(4176): ====>>>>>Num Sensor: 2
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 3
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 4
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 5
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 6
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 7
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 8
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 9
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 10
03-08 14:27:59.068: D/SensorManager(4176): ====>>>>>Num Sensor: 0
03-08 14:27:59.068: D/SensorManager(4176): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 20000 Listener= maps.h.a@414cb1e0
03-08 14:27:59.068: E/SensorManager(4176): thread start
03-08 14:27:59.068: E/SensorManager(4176): =======>>> Sensor Thread Running <<<========
03-08 14:27:59.208: D/libEGL(4176): loaded /system/lib/egl/libEGL_mali.so
03-08 14:27:59.213: D/libEGL(4176): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-08 14:27:59.218: D/libEGL(4176): loaded /system/lib/egl/libGLESv2_mali.so
03-08 14:27:59.228: D/(4176): Device driver API match
03-08 14:27:59.228: D/(4176): Device driver API version: 10
03-08 14:27:59.228: D/(4176): User space API version: 10 
03-08 14:27:59.233: D/(4176): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Mon Mar  5 09:47:55 KST 2012 
03-08 14:27:59.303: D/dalvikvm(4176): GC_FOR_ALLOC freed 143K, 3% free 10004K/10247K, paused 14ms
03-08 14:27:59.368: D/dalvikvm(4176): GC_CONCURRENT freed 194K, 4% free 10276K/10631K, paused 1ms+2ms
03-08 14:27:59.418: D/dalvikvm(4176): GC_FOR_ALLOC freed 598K, 8% free 10022K/10823K, paused 23ms
03-08 14:27:59.523: D/dalvikvm(4176): GC_CONCURRENT freed 365K, 7% free 10162K/10823K, paused 2ms+3ms
03-08 14:27:59.643: D/dalvikvm(4176): GC_CONCURRENT freed 579K, 7% free 10094K/10823K, paused 2ms+3ms
03-08 14:27:59.783: D/dalvikvm(4176): GC_CONCURRENT freed 241K, 5% free 10338K/10823K, paused 2ms+2ms
03-08 14:27:59.938: D/dalvikvm(4176): GC_FOR_ALLOC freed 92K, 4% free 10464K/10823K, paused 26ms
03-08 14:27:59.943: I/dalvikvm-heap(4176): Grow heap (frag case) to 11.271MB for 1048592-byte allocation
03-08 14:27:59.973: D/dalvikvm(4176): GC_FOR_ALLOC freed 46K, 4% free 11442K/11911K, paused 18ms
03-08 14:28:00.023: D/dalvikvm(4176): GC_CONCURRENT freed 1K, 4% free 11443K/11911K, paused 2ms+2ms
03-08 14:28:00.108: D/dalvikvm(4176): GC_FOR_ALLOC freed 1090K, 13% free 10647K/12103K, paused 21ms
03-08 14:28:00.108: I/dalvikvm-heap(4176): Grow heap (frag case) to 11.449MB for 1048592-byte allocation
03-08 14:28:00.133: D/dalvikvm(4176): GC_FOR_ALLOC freed <1K, 4% free 11670K/12103K, paused 14ms
03-08 14:28:00.193: D/dalvikvm(4176): GC_CONCURRENT freed 21K, 4% free 11677K/12103K, paused 1ms+2ms
03-08 14:28:00.293: D/dalvikvm(4176): GC_FOR_ALLOC freed 1288K, 13% free 10649K/12167K, paused 16ms
03-08 14:28:00.293: I/dalvikvm-heap(4176): Grow heap (frag case) to 11.452MB for 1048592-byte allocation
03-08 14:28:00.308: D/dalvikvm(4176): GC_FOR_ALLOC freed 174K, 6% free 11499K/12167K, paused 16ms
03-08 14:28:00.358: D/dalvikvm(4176): GC_CONCURRENT freed 1K, 5% free 11603K/12167K, paused 1ms+2ms
03-08 14:28:00.458: D/dalvikvm(4176): GC_FOR_ALLOC freed 1084K, 14% free 10570K/12167K, paused 40ms
03-08 14:28:00.458: I/dalvikvm-heap(4176): Grow heap (frag case) to 11.374MB for 1048592-byte allocation
03-08 14:28:00.498: D/dalvikvm(4176): GC_FOR_ALLOC freed 82K, 6% free 11511K/12167K, paused 31ms
03-08 14:28:00.548: D/dalvikvm(4176): GC_CONCURRENT freed 19K, 6% free 11514K/12167K, paused 2ms+3ms

1 个答案:

答案 0 :(得分:2)

进入设置 - &gt; “位置访问” 确保这一点 启用“Wi-Fi和移动网络位置”设置。