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 分も待ちたくありません。アクティブなインターネット接続があり、null の場所で数分間スパムが送信されることはありません。位置情報は私にとってミステリーであり、私が望んでいることは決してなく、常にランダム性で私を驚かせます。私はそれをさまざまな方法で構築しましたが、うまく機能するコードを作成することはできません。

助けてください、何を変更すればよいですか?

4

1 に答える 1

0

なぜあなたは使わないのですgetLastKnownLocationか?

于 2013-01-31T14:50:21.913 に答える