2012-06-26 43 views
2

是否有可能將android.location.AddressIntent的實例加入其他活動?如何發送android.location.Address作爲putextra的意圖

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

如果上述策略都不支持「Address」,那麼將Address實例傳遞給另一個活動的最佳方法是什麼? 如果我聲明, public static Address current_location;

並嘗試從其他活動訪問它,那麼不能保證Android將保存current_location數據。或者我錯了?

在這種情況下,應遵循什麼方法?

回答

5

你會想要通過它作爲parcelable。通過查看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); 

密鑰只是您用來存儲parcelable的名稱。

2

我不知道你是否可以通過一個地址(見下文),但爲什麼不只是傳遞經度和緯度(都可以作爲額外的double s),然後重建它下一個活動?

地址似乎實現了Parcelable接口,這意味着您可以將其作爲intent.putParcelable(key, address)發送。

+0

這對我來說不起作用,對於我發送數據的Activity類來幫助用戶找到儘可能最佳的搜索結果,所以我需要來自Address實例的所有可能的信息。 –

+0

好的。你有沒有嘗試過Parcelable方法?我沒有用太多,但我相信你能在這裏找到一個指南。 –

+0

我已經試過了,它工作。我很欣賞你的指導方針 –