如何将意图发送到putextra的android.location.Address

Riz*_*man 2 static android bundle android-intent start-activity

是否有可能把一个实例android.location.Address与Intent另一个活动?

Address current_location;
Run Code Online (Sandbox Code Playgroud)

我的意思是,我试图通过以下方式直接将current_location置于意图中:

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

  2. 或通过捆绑间接:

    Bundle b = new Bundle();
    b.put....("current_location",current_location);
    IntentName.put(b);
    
    Run Code Online (Sandbox Code Playgroud)

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

并尝试从另一个活动访问它,然后无法保证Android将保留current_location数据.还是我错了?

在这种情况下,应采取何种方法?

jer*_*rts 5

你会希望将它作为一个可以通过的传递.通过查看API,我认为它们已经符合标准.

Address extends Object实现了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 )
    );
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中重新组装它你会做:

   String key = getResources().getString(key);
   Address current_location = getIntent().getExtras().getParcelable(key); 
Run Code Online (Sandbox Code Playgroud)

在关键的仅仅是用来保存下parcelable名称.