Why has my NETWORK_PROVIDER suddenly stopped calling onLocationChanged?

这一生的挚爱 提交于 2019-12-13 10:24:16

问题


My NETWORK_PROVIDER in my GPS application has stopped working all of a sudden. It has nothing to do with my internet connection because every other application on my phone works with the internet. I tried it with GPS_PROVIDER and it works perfectly fine and on top of that, after calling onLocationChanged it IS able to retrieve locations from the internet, so this further reinstates that the internet WORKS.

My phone:

  • Samsung Galaxy S2
  • Android version 4.0.3

Things I have tried to no avail:

  • Cleaned the project
  • Testing outside in different locations. Same again, GPS works but Network provider does not.
  • Rebooting phone/Switching phone on off
  • Uninstalling app on phone then reinstalling
  • Turning internet on and off

I repeat, everything was working fine and then all of a sudden it stopped working for no reason. I did not change my code in anyway at all during this transition.

Nevertheless, here is parts of my code and here it is in pastebin http://pastebin.com/itrXgWr7:

This is called first, it is able to enter if(networkEnabled) and then in the LogCat it prints out "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);
    }
}

This is the LocationListener. onLocationChanged is never called with NETWORK_PROVIDER. I repeat again, it USED to call it but not anymore. What happens instead when I turn off GPS is that onProviderDisabled is called straight away, giving me a toast message of "Disabled provider network". This is with Wifi and mobile internet turned ON:

    //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
}

I have all necessary permissions in my XML file as you can see:

<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"/>

This is my 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:


Go into Settings -> "Location access" Make sure that "Wi-Fi & mobile network location" setting is enabled.



来源:https://stackoverflow.com/questions/15296611/why-has-my-network-provider-suddenly-stopped-calling-onlocationchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!