6

Have you written a utility to print all parameters of an intent? Like...

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

including target, extras, data, etc ? Thanks for the help.

UPDATE: What I have so far ...

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; 
}
kandinski
  • 860
  • 1
  • 8
  • 13
  • 2
    Why don't you use the debugger? It's way easier and more universal – Robin Kanters Sep 16 '15 at 15:29
  • 1
    see `toUri(int flags)` method – pskink Sep 16 '15 at 15:34
  • @pskink: The URI contains the Intent's data as the base URI, with an additional fragment describing the action, categories, type, flags, package, component, and extras. That looks perfect, however... how do I get the "additional fragment?" I just get ... "intent:#Intent;component=com" – kandinski Sep 16 '15 at 16:01
  • @ Robin Kanters: Just personal preference, I like to watch logs. :) They remind me of my programs flow of execution and usually serve as good places to write tests when I'm done. That said, I could probably take your advice more often... maybe I would get faster using it. – kandinski Sep 16 '15 at 16:07
  • I tried all the flags. All I ever get is the target class. The doc says there is an "additional fragment" with all the good stuff. How do you get to it? – kandinski Sep 16 '15 at 16:11
  • see [this](http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/content/Intent.java#7288) – pskink Sep 16 '15 at 16:50
  • @kandinski The additional fragment is actually the part "describing the action, categories, type, flags, package, component, and extras", so actually most of the output of the _toUri_ method. See [this answer](http://stackoverflow.com/a/39543548/220039) for an example. – TechAurelian Sep 17 '16 at 07:03
  • see answers here https://stackoverflow.com/questions/5968896/listing-all-extras-of-an-intent – Sir Codesalot Dec 21 '17 at 14:45

2 Answers2

14

The Intent#toString() method works pretty good, it will print most stuff but it doesn't print the extras unfortunately. The extras are in a Bundle that can also be printed with Bundle#toString() but if the Intent just arrived from another process then the contents of the extras Bundle won't be printed until you trigger it to be unparcelled, also it doesn't properly print lists or arrays. This code below should help print out just about everything:

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();
}
satur9nine
  • 13,927
  • 5
  • 80
  • 123
6

The simplest one:

 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();

}
Yan
  • 1,569
  • 18
  • 22
  • But this doesn't include the category and flags, and the easiest way to log those is to just use intent.toString, so I think it is just the extras bundle that requires special handling. – Tom Aug 16 '17 at 16:49