1

私はAndroid開発の初心者です。アクティビティ間の GPS 位置を管理しようとしています。特に、メイン アクティビティで開始されたスレッドを作成しました。このスレッドは、数間隔後に GPS 位置を更新し、新しい位置を共有 Bean に保存します。ここで、Bean をエクストラとして次のアクティビティに渡すと、Bean の最後の値を取得できますが、新しいアクティビティの Bean はスレッドによって更新されません。私は新しい Bean を作成しません。このため、新しいアクティビティで Bean の更新が見られると思います。新しいアクティビティでエクストラを取得するために使用するコードがあります。

    ShareBean pos;
    Intent intent = getIntent();
    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {
        pos = (ShareBean)intent.getSerializableExtra("Location");
    }

どんな助けでも大歓迎です。よろしくお願いします。シモーネ

4

1 に答える 1

0

オブジェクトを使用して、LocationManager場所の更新を取得してアクセスする必要があります。迅速な更新のために、最後の既知の場所を照会できます。

重要なことは、ロケーション マネージャーに話を聞いてもらい、そこからいつでも簡単な最新情報を求めることができるということです。更新された位置情報をオブジェクト (ローカルで ApplicationContext呼び出す) に保存します。これは、オブジェクトの存続期間を通じて永続的です。appModel

私はこのLocationManagerように使用します:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
startListening();

リスニングを開始すると、次のようになります。

public void startListening() {

    if (gpsLocationListener == null) {
        // make new listeners
        gpsLocationListener = new CustomLocationListener(LocationManager.GPS_PROVIDER);

        // request very rapid updates initially. after first update, we'll put them back down to a much lower frequency
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 200, gpsLocationListener);
    }

    //get a quick update
    Location networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    //this is the applicationContext object which persists for the life of the applcation
    if (networkLocation != null) {
        appModel.setLocation(networkLocation);
    }
}

ロケーション リスナーは次のようになります。

private class CustomLocationListener implements LocationListener {

    private String provider = "";
    private boolean locationIsEnabled = true;
    private boolean locationStatusKnown = true;

    public CustomLocationListener(String provider) {
        this.provider = provider;
    }

    @Override
    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        handleLocationChanged(location);
    }

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

    public void onProviderEnabled(String provider) {
        startListening();
    }

    public void onProviderDisabled(String provider) {
    }
}

private void handleLocationChanged(Location location) {

    if (location == null) {
        return;
    }

    //get this algorithm from: http://developer.android.com/guide/topics/location/obtaining-user-location.html
    if (isBetterLocation(location, appModel.getLocation())) {
        appModel.setLocation(location);
        stopListening();
    }
}

幸運を!

于 2011-07-29T19:05:28.853 に答える