如果您无法记住所有键的名称(即使仅能打印键名也很酷),那么是否有一种简单的方法将 Bundle
的内容打印到Logcat中?
如果您无法记住所有键的名称(即使仅能打印键名也很酷),那么是否有一种简单的方法将 Bundle
的内容打印到Logcat中?
Bundle#keySet()应该正常工作。
for (String key: bundle.keySet())
{
Log.d ("myApplication", key + " is a key in the bundle");
}
如果您想获取Object,可以使用Bundle#get(String key)
(也在我回答顶部链接的同一文档中)。但请记住使用通用的get()
方法:
toString()
将被调用,一切都会很好。但是,如果您真正想要使用键的值对,则需要进行instanceof
检查,以避免调用错误的方法。您可以通过以下方式打印映射值以获得更具体的结果:
for (String key : bundle.keySet())
{
Log.d("Bundle Debug", key + " = \"" + bundle.get(key) + "\"");
}
捆绑成字符串转换器:
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
注意箭头=>
和分号;
可以让你在键和值中提到空格。箭头前一个空格,箭头后一个空格,在分号前没有空格,分号后一个空格,在{
之后一个空格,在}
之前一个空格,而所有其他空格都存在于键或值中。 return buf.toString();
}`
- Maurix(这些说明来自于Android Studio 3.1.3(2018年6月)...
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的变量时,您将在调试器窗口的变量部分中看到从上述代码生成的输出。
keySet(); toString()
,因为访问 Bundle 的内容似乎会使其具体化,从而让 toString()
格式化所有元素。请参见我的答案。 - Jerry101Kotlin 中的简单 Bundle 转字符串实现:
val bundleToString = bundle.keySet()
.joinToString(", ", "{", "}") { key ->
"$key=${bundle[key]}"
}
结果示例 {id=3, name="Jhon"}
keySet()
返回排序后的集合)。 - CoolMind/**
* 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()) {
when (val value = this[key]) {
is Bundle -> value.printDebugLog(key)
is Array<*> -> Log.d("printDebugLog", "$parentKey.$key : ${value.joinToString()}")
else -> Log.d("printDebugLog", "$parentKey.$key : $value")
}
}
}
}
myBundle.printDebugLog()
bundle.toString()
将格式化其元素。(仅用于调试目的。)newOptions.keySet()
。 @Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle newOptions) {
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
newOptions.keySet(); // <-- reify the Bundle so .toString() will elaborate
Log.i(TAG, "*** WidgetOptionsChanged: " + newOptions);
}
结果:
*** WidgetOptionsChanged: Bundle[{appWidgetMaxHeight=137, appWidgetCategory=1, appWidgetMaxWidth=603, appWidgetMinHeight=82, appWidgetMinWidth=338, appWidgetSizes=[338.0x137.5, 603.5x82.0]}]
*** WidgetOptionsChanged: Bundle[mParcelledData.dataSize=408]
如果您只有简单的Bundle(没有嵌套的bundle),那么Jerry101的提示已经可以帮助您解决大部分问题了(请查看他们的评论以了解为什么这样做有效)。以下是一行代码:
Log.d(TAG, "bundle: ${bundle?.also { it.keySet() }?: "N/A"}")
Log.d(TAG, "bundle: ${bundle?.keySet()?.associateWith { bundle[it] }?: "N/A"}")
这将bundle转换为map并打印
bundle: {a=42, s=hello, ...}
bundle.keySet().stream().forEach(k -> Log.d(TAG, k + " = " + bundle.get(k)));
val bundleFromNotifications: Bundle? = remoteMessage?.toIntent()?.extras
bundleFromNotifications?.keySet()?.forEach{
Log.d(LOG_TAG, it + "=> \"" + bundleFromNotifications.get(it) + "\"")
}
bundle.keySet()
(因为你提供的文档链接没问题);否则这个是正确的。 - Cat