0

我做了一个应该得到纬度和经度的后台服务,但由于某种原因它不能正常工作:

public class BackgroundLocationService extends IntentService {

private static final String TAG = "Location Service";

private LocationManager locManager;
private LocationListener locListener = new MyLocationListener();
public static Context context;

private String latitude;
private String longitude;

Messenger messenger;
Timer t = new Timer();

public BackgroundLocationService() {
    super("backgroundLocationService");

    locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationProviderInit();
}

private void locationProviderInit() {

    boolean gps_enabled;
    boolean network_enabled;

    gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (gps_enabled) {
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

    }
    if (network_enabled) {
        locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);

    }
}

@Override
protected void onHandleIntent(Intent intent) {
    messenger = (Messenger) intent.getExtras().get("handler");

    t.schedule(new TimerTask() {

        @Override
        public void run() {

            locationProviderInit();

            if (latitude != null && longitude != null) {

                Log.i(TAG, "Lat and Long are: "+latitude+" - "+longitude);
            } else {
                Log.i(TAG, "Lat and Long are null");
            }
        }
    }, 0, 7000);

}



class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {

        if (location != null) {
            longitude = Double.toString(location.getLongitude());
            latitude = Double.toString(location.getLatitude());
        }
    }

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

MyLog 猫:

01-31 15:26:54.639: I/Location Service(2347): Lat and Long are null
01-31 15:26:58.057: I/Location Service(2347): Lat and Long are null
01-31 15:27:01.643: I/Location Service(2347): Lat and Long are null
01-31 15:27:05.057: I/Location Service(2347): Lat and Long are null
01-31 15:27:08.639: I/Location Service(2347): Lat and Long are null
01-31 15:27:12.065: I/Location Service(2347): Lat and Long are null
01-31 15:27:15.643: I/Location Service(2347): Lat and Long are null
01-31 15:27:19.053: I/Location Service(2347): Lat and Long are null
01-31 15:27:22.639: I/Location Service(2347): Lat and Long are null
01-31 15:27:26.057: I/Location Service(2347): Lat and Long are null
01-31 15:27:29.643: I/Location Service(2347): Lat and Long are null
01-31 15:27:33.061: I/Location Service(2347): Lat and Long are null
01-31 15:27:36.639: I/Location Service(2347): Lat and Long are null
01-31 15:27:40.061: I/Location Service(2347): Lat and Long are null

大约 3-5 分钟后,它得到了位置:

01-31 15:34:48.404: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996
01-31 15:34:55.408: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996
01-31 15:35:02.404: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996
01-31 15:35:09.400: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996
01-31 15:35:16.400: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996
01-31 15:35:23.400: I/Location Service(2417): Lat and Long are: 47.479290815904015 - 19.086711048181996

我怎样才能让它更快?我不想在获得位置之前等待 5 分钟。有活跃的互联网连接,它不应该在空位置的几分钟内向我发送垃圾邮件。定位对我来说是个谜,它永远不会为我做我想要的事情,而且总是让我感到随机性。我以多种方式构建了它,但我无法编写一个很好的代码。

请帮忙,我应该改变什么?

4

1 回答 1

0

你为什么不使用getLastKnownLocation

于 2013-01-31T14:50:21.913 回答