Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print the contents of an Intent for logging purposes?

Tags:

android

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; 
}
like image 825
kandinski Avatar asked Sep 16 '15 15:09

kandinski


People also ask

What is intent explain with example?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.

How is data stored in intent?

We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What is an intent filter?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.


2 Answers

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();
}
like image 152
satur9nine Avatar answered Sep 18 '22 01:09

satur9nine


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

}
like image 21
Yan Avatar answered Sep 19 '22 01:09

Yan