如何检测位置提供程序是否可访问或无法访问?

时间:2014-08-04 03:31:21

标签: android android-activity android-location

我正在开发一个Android应用程序,如果提供程序的状态发生变化,我需要记录。如果无法访问wifi连接,则应该有状态更改的日志条目。

provider = locationManager.getBestProvider(crit, true);
isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        Log.i("GPSTracker", "isGPSEnabled-" + isGPSEnabled);
        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        Log.i("GPSTracker", "isNetworkEnabled-" + isNetworkEnabled);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {

            //Log.i("GPSTracker", "provider=" + provider);
            this.canGetLocation1 = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                Log.i("GPSTracker", "line 65 network enabled");
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                Log.i("GPSTracker", "gps enabled line 82");
                if (location == null) {
                    Log.i("GPSTracker",
                            "location is null. request gps provider");
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.v("GPSTracker", "Status Changed: Out of Service");
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v("GPSTracker", "Status Changed: Temporarily Unavailable");
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.v("GPSTracker", "Status Changed: Available");
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }
}

现在我试图远离wifi,直到没有wifi信号。现在我期待状态回调的改变,这是没有发生的。我看到没有出现日志或Toast消息。

0 个答案:

没有答案