Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Print the contents of a Bundle to Logcat?

Is there an easy way to print the contents of a Bundle to Logcat if you can't remember the names of all the keys (even being able to print just the key names would be cool)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Bundle#keySet() should work.

for (String key: bundle.keySet())
{
  Log.d ("myApplication", key + " is a key in the bundle");
}

And if you want to get the Object, you can use Bundle#get(String key)(which is also in the same documentation I linked at the top of my answer). However, keep in mind using the generic get() call:

  • You're working with Object. If you're simply printing to a Log, toString() will be invoked and all will be fine. However, if you actually want to use the key's pair, you need to do instanceof checks to avoid calling the wrong method.
  • Since toString will be invoked, if you have a special Object (eg ArrayLists, or special Serializable/Parcelable extras) you're most likely not going to get anything useful from the printout.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...