有一种简单的方法来打印的内容Bundle
到logcat的,如果你不能记住所有的按键的名称(甚至能够打印只这些按键的名称会很酷)?
Answer 1:
束#键设置()应该工作。
for (String key: bundle.keySet())
{
Log.d ("myApplication", key + " is a key in the bundle");
}
如果你想要得到的对象,你可以使用Bundle#get(String key)
(这也是我在我的答案的上方连接的同一个文档中) 。 但是,请记住使用通用get()
调用:
- 你有对象的工作。 如果你只是打印到日志,
toString()
将被调用,一切都会好起来的。 但是,如果你真的想使用密钥的对,你需要做instanceof
检查,以避免调用错误的方法。 - 由于的toString将被调用,如果你有一个特殊的对象(例如的ArrayList,或特殊的序列化/ Parcelable演员)你最有可能不会得到任何东西从打印输出有用。
Answer 2:
您可以通过打印映射值如下得到更具体:
for (String key : bundle.keySet())
{
Log.d("Bundle Debug", key + " = \"" + bundle.get(key) + "\"");
}
Answer 3:
管束对串转换器:
public static String bundle2string(Bundle bundle) {
if (bundle == null) {
return null;
}
String string = "Bundle{";
for (String key : bundle.keySet()) {
string += " " + key + " => " + bundle.get(key) + ";";
}
string += " }Bundle";
return string;
}
实例:
Log.d(TAG,"details="+bundle2string(details));
输出:
details=Bundle{ RESPONSE_CODE => 5; }Bundle
请注意,箭头=>
和分号;
让你提到的键和值空间。 箭头前面有一个空格,箭头后有一个空格,分号前面没有空格,分号后一个空格,后一个空间{
和前一个空间}
,和其他所有空间都存在,因为他们中的键或值。
Answer 4:
要知道,这是不完全回答这个问题,但我看到的开发商配发试图转储内容的logcat /控制台,因为他们不知道,他们可以在Android的Studio调试器设置为显示自定义对象在调试实时渲染,当你打一个破发点。 而在捆绑的情况下,可以采取在这里其他的答案显示代码的类型和应用,作为一个自定义渲染,让你不必管转储到logcat的和/或控制台。
(这些指令是在Android 3.1.3工作室(2018年6月)...
- 选择“文件”,然后在“设置”菜单选项/子选项。
- 在“设置”对话框,在左侧,向下钻取,然后选择“建立,实施,部署”,“调试”,“数据视图”,“Java类型渲染器”。
- 对话框,它说:“渲染器名称”中输入你想找出与您正在创建的渲染器的名称的右侧。
- 对话框,它说:“应用渲染器类型的对象”,进入右侧的“android.os.Bundle”。
- 对话框的右侧,“当渲染节点”部分下,选择“使用下面的表达式:”单选按钮。
- 在下面的文本字段中,键入以下...
StringBuilder builder = new StringBuilder(); for (String key : ((android.os.Bundle)this).keySet()) { Object value = ((android.os.Bundle)this).get(key); builder.append("["); builder.append(key); builder.append("]=["); builder.append(value); builder.append("]("); builder.append((value != null) ? value.getClass().getSimpleName() : "null"); builder.append("), "); } return builder.toString();
- 按“应用” /“确定”按钮。
现在,当你运行你的应用程序,你打一个断点,显示一个变量的类型是android.os.Bundle的,你会看到从上面的代码生成的输出,在调试器窗口的变量部分。
我还将包括一个屏幕截图,展示了上述我...
Answer 5:
在科特林,递归的,当它包含子包:
/**
* Recursively logs the contents of a [Bundle] for debugging.
*/
fun Bundle.printDebugLog(parentKey: String = "") {
if (keySet().isEmpty()) {
Log.d("printDebugLog", "$parentKey is empty")
} else {
for (key in keySet()) {
val value = this[key]
when (value) {
is Bundle -> value.printDebugLog(key)
is Array<*> -> Log.d("printDebugLog", "$parentKey.$key : ${value.joinToString()}")
else -> Log.d("printDebugLog", "$parentKey.$key : $value")
}
}
}
}
用法: myBundle.printDebugLog()
Answer 6:
我已经开发了一种名为库pretty-print
这些批注处理器,打印在一个不错的表格式的包的内容。 请检查出来https://github.com/NULLPointerGuy/pretty-print
Answer 7:
一个科特林的解决方案:
val bundleFromNotifications: Bundle? = remoteMessage?.toIntent()?.extras
bundleFromNotifications?.keySet()?.forEach{
Log.d(LOG_TAG, it + "=> \"" + bundleFromNotifications.get(it) + "\"")
}
文章来源: Print the contents of a Bundle to Logcat?