0

現在、サーバーのGPSロケーションへの遅延送信を行っています。場所 (緯度と経度) は 3 分ごとに送信する必要があります。現在、テスト目的で 20 秒ごとに送信するように設定されており、出力を検証するためだけに座標をログに記録しています。ここでの問題は、エミュレーターで場所を地理的に修正しているときに (テストするデバイスがない場合)、ロガー クラスが最後の場所だけでなく、最新の場所をすべて出力することです。postDelayed のハンドラーが機能します。これが私のクラスです。

このコードは @kyogs からのものです。

public class LocalizadorGps extends Service {
    private LocationManager mlocmag;
    LocationListener mloclist;
    private  long UPDATE_INTERVAL;
    private double latn,longn;
    public Location loc;

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mlocmag = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mloclist = new MyLocationList();

        loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (loc == null) {
            loc = mlocmag.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }

        updateServer(loc);
        mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 1000,mloclist);


    }

    public void updateServer(final Location loc) {
        final Handler handler = new Handler();

        Runnable runnable = new Runnable() {
            public void run() {
                if (loc != null) {
                    final double latitude = loc.getLatitude();
                    final double longitude = loc.getLongitude();
                    Log.v("COORDINATES", Double.toString(latitude) + " " + Double.toString(longitude));
                } else {
                    System.out.println("Location not avilable");
                }

                handler.postDelayed(this, 20000);
            }
        };
        handler.postDelayed(runnable, 20000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mlocmag.removeUpdates(mloclist);
    }

    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }

    public class MyLocationList implements LocationListener {

        public void onLocationChanged(Location location) {
            updateServer(location);
        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    }
}

私はこれをやっています:

geo fix 44.41 56.75

出力は次のとおりです。

06-27 04:03:55.736  13743-13743/com.example.testingui          
V/COORDINATES: 56.75 44.409998333333334

次に、もう 1 つの場所を修正します。

geo fix 44.44 80.33

出力は次のとおりです。

06-27 04:04:15.756  13743-13743/com.example.testingui          
V/COORDINATES: 56.75 44.409998333333334
06-27 04:04:19.725  13743-13743/com.example.testingui          
V/COORDINATES: 80.32999833333334 44.43999833333334

以前に固定された場所と新しい場所を繰り返します。望ましい動作は、最後の場所です。

そして最後のもの:

geo fix 44.44 33.67

出力:

06-27 04:04:35.767  13743-13743/com.example.testingui          
V/COORDINATES: 56.75 44.409998333333334
06-27 04:04:39.686  13743-13743/com.example.testingui          
V/COORDINATES: 33.669999999999995 44.43999833333334
06-27 04:04:39.736  13743-13743/com.example.testingui          
V/COORDINATES: 80.32999833333334 44.43999833333334

最後の 3 つの固定位置を繰り返します。(出力の時間を見てください)。繰り返しますが、望ましい動作は、最後に修正された場所です。

注:これをハンドラーの代わりにタイマーでテストしたところ、同じ結果が得られました!

ここでの私の質問は次のとおりです。

私は何かひどく間違ったことをしていますか?問題が見つかりません:(。

4

2 に答える 2

0

実際にスレッドを閉じているようには見えません。毎回新しい Runnable をスピンアップしないようにしましたか?

于 2013-07-02T23:03:20.623 に答える
0

It looks like you're queuing up the same Runnable twice via two handler.postDelayed calls, so the second one queued from within the Runnable.run() is echoing same output from the first execution of the same Runnable.

This code:

Runnable runnable = new Runnable() {
        public void run() {
            if (loc != null) {
                final double latitude = loc.getLatitude();
                final double longitude = loc.getLongitude();
                Log.v("COORDINATES", Double.toString(latitude) + " " + Double.toString(longitude));
            } else {
                System.out.println("Location not avilable");
            }

            handler.postDelayed(this, 20000);
        }
    };
    handler.postDelayed(runnable, 20000);

...should be changed to:

Runnable runnable = new Runnable() {
        public void run() {
            if (loc != null) {
                final double latitude = loc.getLatitude();
                final double longitude = loc.getLongitude();
                Log.v("COORDINATES", Double.toString(latitude) + " " + Double.toString(longitude));
            } else {
                System.out.println("Location not avilable");
            }
        }
    };
    handler.postDelayed(runnable, 20000);
于 2013-07-03T20:02:04.043 に答える