51

I'm having a heck of a time figuring out what data is coming to my methods through Intent/Bundles. I've tried adding break points to inspect the data, but I don't see anything. Perhaps because it's a Parcelable I can't manually read it in Eclipse.

For instance, a onActivityResult(int requestCode, int resultCode, Intent data) for a Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI). How do I know what data is available? Notice, I'm not ask for WHAT data is available but how the heck do I figure it out so I can apply the same idea to any Bundle/Intent from the Android framework? Perhaps it's a simple as looking at the docs, but I don't see a full listing of the data and I can't see it in Eclipse. So I'm at a lost.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user123321
  • 12,593
  • 11
  • 52
  • 63

4 Answers4

98

Bundle.keySet() gives you a list of all keys in the bundle. That said, typically you just expect certain keys and query them, but keySet() is used to examine bundles you get from somewhere.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • thanks. How do i KNOW what do expect is the question? I see now documentation on it, is there? – user123321 Jun 25 '11 at 03:16
  • A bundle is really just a container. It entirely depends on the app that packaged it up. If it's your own, then it's obvious - it's whatever you put in. If it's somebody else's (or from the operating system), then it depends on what said app put in. Option 1: Check the documentation. Option 2: Ask the developer of the app. Option 3: Use Bundle.keySet() to analyze an incoming bundle. – EboMike Jun 25 '11 at 04:18
  • **WHERE** is the documentation for the stuff in a Bundle for ContactsContract.Contacts.CONTENT_URI started with an startActivityForResult? – user123321 Jun 28 '11 at 04:23
  • It's not specified in the docs, as such: Don't rely on it. You can try to be smart and use `Bundle.keySet` to see what's inside the Bundle after using Google's contacts app. But guess what: Chances are, HTC's contacts app returns a different bundle. Or MotoBlur. In your particular case, the only thing you can rely on is the intent's data, so read the result as outlined here: http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list .. What information other than the ID do you need anyway? – EboMike Jun 28 '11 at 04:49
  • Seems unlikely there is a different set of "standard" data returned depending on Environment. I don't care about any specific Environment. I want to know who to know what data is available for any of my onCallback(Intent data) handlers. Looping through the keys seems pretty generic and I can't believe Google would leave it to developers to figure out for themselves. – user123321 Jun 28 '11 at 04:57
  • It is very likely. MotoBlur for example even took the extra step and changed how birthdays are stored in the contacts for no good reason. It's extremely frustrating. So yes, if it's not in the API documentation, DO NOT USE IT. Again, in case of contacts, you get the ID via `getData`, that's defined and that you can rely on. The bundle, no. This is meant to be used internally in your own app. Or, if it's essential to a callback, it will be documented. But if not, then not. – EboMike Jun 28 '11 at 05:00
  • I don't want to use any custom shit. Please understand what I'm saying. WHERE IS THE DOCUMENTATION? – user123321 Jun 28 '11 at 06:31
  • I'll try one more time. THERE IS NO DOCUMENTATION. IT IS UNDOCUMENTED. YOU ARE NOT SUPPOSED TO USE ANY UNDOCUMENTED EXTRAS. You can analyze a bundle, or you can look at the Android source code for the contacts app, but then it'll break on other contacts apps. You're only supposed to use getData() to retrieve the contact ID in the example you mentioned. There is documentation on what a bundle is, and how to use it, there is documentation on ACTION_PICK and startActivityForResult, but not for the particular bundle you get with ACTION_PICK on a contact, if any. – EboMike Jun 28 '11 at 08:24
  • It's not about just contacts. The intent of my question is much more general. If there is no documentation then how do I know what data I get from an AlarmManager.set() or a registerReceiver(TelephonyManager.ACTION_PHONE_STATE_CHANGED) or ANY system broadcast or ANY system service or ANY system activity result. – user123321 Jun 29 '11 at 01:22
  • what I said still applies: If it's not documented, don't expect anything. In case of AlarmManager, it depends on whoever created the PendingIntent for the alarm. In case of ACTION_PHONE_STATE_CHANGED, it's clearly defined in the documentation (look at the "EXTRA_" defines here: http://developer.android.com/reference/android/telephony/TelephonyManager.html). There isn't one place with all intents and bundles listed. It's an open system that can be used by anyone (system and users), so you'll find extras specific to intents/classes in the respective documentation. – EboMike Jun 29 '11 at 03:13
  • Thanks. That's a shame. Can you give me any tips then (besides the getKeys()) for how to figure this stuff out? The application I'm currently working on requires heavy use of BroadcastReceivers coming from the OS (Alarm, Boot, Call, Battery, etc....) and would really like to know what properties I have available to me without having to post a question for every single one. Thanks for all the help thus far. Super appreciated!!!!!!!! – user123321 Jun 29 '11 at 21:30
  • The bigger question is - what information do you need from the broadcast? You should base your code on that. In most cases, getData() is all you need. In some cases, you need extras, and those are documented. What specific case is there where you need additional information from a bundle but don't have the documentation for it? – EboMike Jun 29 '11 at 22:33
54
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;
}
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
10

i getting alll key and value of bundle stored...

for (String key : bundle.keySet()) {
    string += " " + key + " => " + bundle.get(key) + ";";
}

output:

(key)       :(value)    
profile_name:abc
Pankaj Talaviya
  • 3,328
  • 28
  • 31
-1

The only thing you get out of a Bundle is what you put in. Bundles are ways of passing information between activities. If you're in charge of your entire application, you shouldn't need to look inside the Bundle for your objects, you should just grab them. Think hashmap keys... if you don't know the key, it's not like you can search the hashmap.

To place an item into a Bundle and pass it on to the next activity, you need to put it as an Extra. Take a look here for an example of passing data via extras and bundles between activities.

Copied and pasted below:

From Activity1

Intent intent = new Intent(this,myActivity2.class);
Bundle bundle = new Bundle();
bundle.putString("myValue", myValue);
intent.putExtras(bundle);
navigation.this.startActivity(intent);

In Activity2

Bundle bundle = getIntent().getExtras();
act2MyValue= bundle.getString("myValue");
Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
John Leehey
  • 22,052
  • 8
  • 61
  • 88