2

android.location.Address是否可以将with的实例放到Intent另一个活动中?

Address current_location;

我的意思是,我试图通过以下方式直接将 current_location 与意图放在一起:

  1. IntentName.putExtra("current_location",current_location);

  2. 或间接通过捆绑:

    Bundle b = new Bundle();
    b.put....("current_location",current_location);
    IntentName.put(b);
    

如果上述策略都不支持“地址”,那么将地址实例传递给另一个活动的最佳方法应该是什么?如果我声明, public static Address current_location;

并尝试从另一个活动中访问它,那么无法保证 Android 将保存 current_location 数据。还是我错了?

在这种情况下,应该采取什么方法?

4

2 回答 2

5

你会想通过它作为一个包裹。通过查看 API,我认为它们已经符合标准。

地址扩展对象实现 Parcelable

https://developer.android.com/reference/android/location/Address.html

因此,要将其打包并发送,您将执行以下操作:

    String key = getResources().getString(key);

    startActivity( new Intent()
                        .setClass(this, YourActivity.class)
                        .putExtra(key, current_location )
    );

然后在您的活动中重新组装它,您将执行以下操作:

   String key = getResources().getString(key);
   Address current_location = getIntent().getExtras().getParcelable(key); 

密钥只是您用来存储包裹的名称。

于 2012-06-26T19:55:03.213 回答
2

我不知道你是否能够传递一个地址(见下文),但为什么不只传递经度和纬度(两者都double可以作为附加项放置),然后在下一个活动中重建它?

Address 似乎确实实现了Parcelable接口,这意味着您可以将其发送为intent.putParcelable(key, address).

于 2012-06-26T19:51:33.413 回答