41

I might have misunderstood how intents are supposed to be used, so I might be asking the wrong thing here. If that's the case, please help me get on the right track with this anyway...

I've just started working on an android app that will poll my server for messages every so often, and when a new message is available, I want to show it to the user. I'm trying to implement this by having a Service that polls the server, and when a new message is received the service should give the message to an Activity that shows it.

To facilitate this communication, I'm trying to create an Intent with ACTION_VIEW, but I can't figure out how to give the message to the activity. Is there no way to pass a string or a regular Java object via the intent?

For what it's worth, this is what I'd like to do:

getApplication().startActivity(new Intent(MessageService.this, ViewMessageActivity.class, message));

but of course, that doesn't even compile.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • are you lokking for the overloaded putExtra() to pass a key/value pair. Later on you can access this data via getExtras() on that intent object – AxelTheGerman Dec 22 '11 at 23:43
  • Instead of polling... if you can, you may want to try to do push messages, polling is NEVER a good idea on a phone. But sometimes we don't have any other option. – JoxTraex Dec 23 '11 at 07:15

3 Answers3

78

Use the Intent bundle to add extra information, like so:

Intent i = new Intent(MessageService.this, ViewMessageActivity.class);
i.putExtra("name", "value");

And on the receiving side:

String extra = i.getStringExtra("name");

Or, to get all the extras as a bundle, independently of the type:

Bundle b = i.getExtras();

There are various signatures for the putExtra() method and various methods to get the data depending on its type. You can see more here: Intent, putExtra.

EDIT: To pass on an object it must implement Parcelable or Serializable, so you can use one of the following signatures:

putExtra(String name, Serializable value)

putExtra(String name, Parcelable value)

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • 1
    This is usually the best way to pass data to an activity. It's really useful especially since putExtra() can take a custom Serializable object. – Krylez Dec 22 '11 at 23:53
23

You can do the following to add information into the intent bundle:

    Intent i = new Intent(MessageService.this, ViewMessageActivity.class);
    i.putExtra("message", "value");
    startActivity(i);

Then in the activity you can retrieve like this:

    Bundle extras = getIntent().getExtras();
    String message = extras.getString("message");
Nick
  • 6,375
  • 5
  • 36
  • 53
2

Starting the activity from your service each time a new message is received might not be what you want. For instance, if you are viewing a different activity you will be interrupted by the new message.

You can use sendBroadcast(intent) along with a BroadcastReceiver to notify an activity that a new message has been received.

benkdev
  • 673
  • 2
  • 16
  • 32
  • 1
    This is probably what I will end up doing, but this was not what I needed to ask at this moment, so I decided to simplify a little =) – Tomas Aschan Dec 22 '11 at 23:51