Android:在activity之间更新bean信息

发布于 2024-11-27 04:16:26 字数 474 浏览 1 评论 0原文

我是 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");
    }

任何帮助都是值得赞赏的。 提前致谢。 西蒙娜

I'm new on Android developing.
I'm trying to manage gps location between activity. In particular I have created a thread, started with main activity, that update after a few interval the gps position and save the new position into a shared Bean.
Now, when I pass as extras the Bean to the next activity, I can get the last value of the bean but, the bean on the new activity isn't updated by the thread. I don't create a new Bean, and for this reason I think that the update of the bean would be seen on the new activity.
There is the code that I use to retrieve the extra in the new activity:

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

Any help is appreciate.
Thanks in advances.
Simone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-12-04 04:16:26

您可能应该使用 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();
    }
}

祝你好运!

you should probably use the LocationManager object to get and access Location updates. You can query it for the last known location for a quick update.

The key things is, I ask the location manager to start listening and then from there I can ask for quick updates anytime. I store the updated location information in my ApplicationContext object (which I call appModel locally) which is persistent throughout the life of the object.

I use the LocationManager like this:

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

Start listening looks like this:

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);
    }
}

You location listener could look like this:

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();
    }
}

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文