Android如何放置地图<;五、 T>;有意
我有一个Android如何放置地图<;五、 T>;有意,android,android-intent,Android,Android Intent,我有一个映射对象,我想把它放在启动服务的意图中: Map<Integer, String> modules = new HashMap<Integer, String>(); Intent downloadServiceIntent = new Intent(context.getApplicationContext(), DownloadService.class); downloadServiceIntent.putExtra("EXTRA_MODULE", modu
映射
对象,我想把它放在启动服务的意图中
:
Map<Integer, String> modules = new HashMap<Integer, String>();
Intent downloadServiceIntent = new Intent(context.getApplicationContext(), DownloadService.class);
downloadServiceIntent.putExtra("EXTRA_MODULE", modules);
context.startService(downloadServiceIntent);
这是个好主意吗?有没有其他方法可以达到这个目的?是的,你的想法很好。没有办法将地图放在意图中。您可以将可序列化设置为额外[1]。例如,HashMap实现了可序列化接口[2]。因此,如果您将变量类型从Map更改为HashMap,您可以直接将Map作为额外变量 [1]
[2] 您是否意识到右束本身就是一张迷你地图?@humblerookie很好的解决方案。谢谢
int[] keys = new int[modules.size()];
String[] values = new String[modules.size()];
int i = 0;
for (Map.Entry<Integer, String> entry : modules.entrySet()) {
keys[i] = entry.getKey();
values[i] = entry.getValue();
i++;
}
downloadServiceIntent.putExtra("EXTRA_KEYS", keys);
downloadServiceIntent.putExtra("EXTRA_VALUES", values);