For debugging reasons I want to list all extras (and their values) of an Intent. Now, getting the keys isn't a problem
Set<String> keys = intent.getExtras().keySet();
but getting the values of the keys is one for me, because some values are strings, some are boolean... How could I get the values in a loop (looping through the keys) and write the values to a logfile? Thanks for any hint!
This is how I define utility method to dump all extras of an Intent.
I noticed in the Android source that almost every operation forces the Bundle to unparcel its data. So if (like me) you need to do this frequently for debugging purposes, the below is very quick to type:
Sorry if this is too verbose or too late, but this was the only way I could find to get the job done. The most complicating factor was the fact that java does not have pass by reference functions, so the get---Extra methods need a default to return and cannot modify a boolean value to tell whether or not the default value is being returned by chance, or because the results were not favorable. For this purpose, it would have been nicer to have the method raise an exception than to have it return a default.
I found my information here: Android Intent Documentation.
Set Documentation
You could use
for (String key : keys) { Object o = get(key);
to return an Object, callgetClass().getName()
on it to get the type, and then do a set of if name.equals("String") type things to work out which method you should actually be calling, in order to get the value?Here's what I used to get information on an undocumented (3rd-party) intent:
(Make sure to check if
bundle
is null before the loop)