我NETWORK_PROVIDER
的 GPS 应用程序突然停止工作。这与我的互联网连接无关,因为我手机上的所有其他应用程序都可以使用互联网。我试过了GPS_PROVIDER
,它工作得非常好,最重要的是,在调用onLocationChanged
它之后,它能够从互联网上检索位置,所以这进一步恢复了互联网的工作。
我的手机:
- 三星盖乐世 S2
- 安卓版本 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);
}
}
这是位置监听器。onLocationChanged 永远不会用NETWORK_PROVIDER
. 我再重复一遍,它曾经叫它,但现在不再叫它了。相反,当我关闭 GPS 时,会立即调用 onProviderDisabled,给我一个 toast 消息"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