Print the contents of a Bundle to Logcat?

后端 未结 10 608
孤独总比滥情好
孤独总比滥情好 2020-12-24 04:47

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 w

相关标签:
10条回答
  • 2020-12-24 05:04

    I have developed a library called pretty-print which annotation processor that prints the contents of the bundle in a nice table format. Do check it out https://github.com/NULLPointerGuy/pretty-print

    0 讨论(0)
  • 2020-12-24 05:05

    Realize that this isn't answering the question exactly, but I see allot of developers trying to dump the contents to logcat/console because they are not aware that they can set up in Android Studio debugger to display customized object rendering at debug time, when you hit a break point. And in the case of Bundle, you can take the type of code shown in the other answers here, and apply that as a custom renderer, so that you don't need to pipe the dump to logcat and/or the console.

    (These instructions are from Android Studio 3.1.3 (June 2018) ...

    1. Select the "File" and then the "Settings" menu option/suboption.
    2. In the 'Settings' dialog, on the left side, drill-down and select "Build, Execution, Deployment", "Debugger", "Data Views", "Java Type Renderers".
    3. Right side of the dialog, where it says "Renderer name" enter a name you wish to identify with the renderer you are creating.
    4. Right side of the dialog, where it says "Apply renderer to objects of type", enter 'android.os.Bundle'.
    5. Right side of the dialog, under the "When rendering a node" section, select the "Use following expression:" radio button.
    6. In the text field below that, type in the following ...
    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();
    
    1. Press 'Apply'/'OK' button.

    Now, when you run your app, and you hit a breakpoint that shows a variable that is of type android.os.Bundle, you'll see the output generated from the above code, on the variables section of the debugger window.

    I'll also include a screenshot, showing what I described above ...

    0 讨论(0)
  • 2020-12-24 05:06

    Bundle-to-string converter:

    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;
    }
    

    Example usage:

    Log.d(TAG,"details="+bundle2string(details));
    

    and output:

    details=Bundle{ RESPONSE_CODE => 5; }Bundle
    

    Note that the arrows => and semicolons ; let you mention spaces in the keys and values. One space before the arrow, one space after the arrow, no space before the semi-colon, one space after the semi-colon, one space after { and one space before }, and all other spaces are there because they are in the keys or values.

    0 讨论(0)
  • 2020-12-24 05:13

    Simple Bundle to String implementation in Kotlin:

    val bundleToString = bundle.keySet()
                .joinToString(", ", "{", "}") { key ->
                    "$key=${bundle[key]}"
                }
    

    Example of result {id=3, name="Jhon"}

    0 讨论(0)
  • 2020-12-24 05:15

    In Kotlin, recursive for when it contains child bundles:

    /**
     * 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")
                }
            }
        }
    }
    

    Usage: myBundle.printDebugLog()

    0 讨论(0)
  • 2020-12-24 05:22

    A Kotlin solution:

    val bundleFromNotifications: Bundle? = remoteMessage?.toIntent()?.extras
    bundleFromNotifications?.keySet()?.forEach{
        Log.d(LOG_TAG, it + "=> \"" + bundleFromNotifications.get(it) + "\"")
    }
    
    0 讨论(0)
提交回复
热议问题