如何打印Intent的内容以进行日志记录?

时间:2015-09-16 15:26:08

标签: android

您是否编写过实用程序来打印意图的所有参数?像...

Intent i = ...    
Log.d(tag, "INTENT = " + Util.printIntent(i) );

包括目标,演员,数据等?谢谢你的帮助。

更新: 我到目前为止......

if (G.DEBUG) Log.d(U.getTag(), "INTENT = " + intentToString(intent)  );

public static String intentToString(Intent intent) {
   if (intent == null) {return null;}
   String out = intent.toString();
   Bundle extras = intent.getExtras();
   if (extras != null) {
       extras.size();
       out += "\n" + printBundle(extras);
   }
   if (intent.getAction() != null)     out+="\nAction = " + intent.getAction();
   if (intent.getType() != null)       out+="\nType = " + intent.getType();
   if (intent.getData() != null)       out+="\nData = " + intent.getData();
   if (intent.getPackage() != null)    out+="\nPackage = " + intent.getPackage();
   if (intent.getDataString() != null) out+="\nDataString = " + intent.getDataString();
return out; 
}

2 个答案:

答案 0 :(得分:7)

Intent#toString()方法工作得很好,它会打印大部分内容,但不幸的是它不打印附加内容。附加内容位于Bundle,也可以使用Bundle#toString()打印,但如果Intent刚从另一个进程打印出来,则在您触发附加内容之前,不会打印附加内容Bundle的内容要取消打包,也不能正确打印列表或数组。下面的代码应该有助于打印出几乎所有内容:

public static String intentToString(Intent intent) {
    if (intent == null) {
        return null;
    }

    return intent.toString() + " " + bundleToString(intent.getExtras());
}

public static String bundleToString(Bundle bundle) {
    StringBuilder out = new StringBuilder("Bundle[");

    if (bundle == null) {
        out.append("null");
    } else {
        boolean first = true;
        for (String key : bundle.keySet()) {
            if (!first) {
                out.append(", ");
            }

            out.append(key).append('=');

            Object value = bundle.get(key);

            if (value instanceof int[]) {
                out.append(Arrays.toString((int[]) value));
            } else if (value instanceof byte[]) {
                out.append(Arrays.toString((byte[]) value));
            } else if (value instanceof boolean[]) {
                out.append(Arrays.toString((boolean[]) value));
            } else if (value instanceof short[]) {
                out.append(Arrays.toString((short[]) value));
            } else if (value instanceof long[]) {
                out.append(Arrays.toString((long[]) value));
            } else if (value instanceof float[]) {
                out.append(Arrays.toString((float[]) value));
            } else if (value instanceof double[]) {
                out.append(Arrays.toString((double[]) value));
            } else if (value instanceof String[]) {
                out.append(Arrays.toString((String[]) value));
            } else if (value instanceof CharSequence[]) {
                out.append(Arrays.toString((CharSequence[]) value));
            } else if (value instanceof Parcelable[]) {
                out.append(Arrays.toString((Parcelable[]) value));
            } else if (value instanceof Bundle) {
                out.append(bundleToString((Bundle) value));
            } else {
                out.append(value);
            }

            first = false;
        }
    }

    out.append("]");
    return out.toString();
}

答案 1 :(得分:2)

最简单的一个:

 public static String intentToString(Intent intent) {
    if (intent == null)
        return "";

    StringBuilder stringBuilder = new StringBuilder("action: ")
            .append(intent.getAction())
            .append(" data: ")
            .append(intent.getDataString())
            .append(" extras: ")
            ;
    for (String key : intent.getExtras().keySet())
        stringBuilder.append(key).append("=").append(intent.getExtras().get(key)).append(" ");

    return stringBuilder.toString();

}