Tuesday, July 31, 2012

How to implement ‘Tell a friend’ action


Many times we need to share a link or other information with our friends or other users. It can be the case when we want to share our application link with other users.

tell a friend
We want to allow the user to chose how to share this information using Facebook, Twitter or simply SMS or email.
The first step is to create an Intent with SEND action like that :

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

Next we need to set the content-type so that the system can chose the application that can handle this content-type, in our case we need text/plain.

sharingIntent.setType("text/plain");

It’s time we set the body of our message and the subject if the application supports it:
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Here the body");
Please notice that in case the user choses for example SMS or Twitter the body shouldn’t be more than 140 chars. For example if you want to send a link you should use a shorten link service.  and the subject (if supoorted)

sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject here");
Now we simply need to launch our action

startActivity(Intent.createChooser(sharingIntent, "How do you want to share?"));

The result is shown below:
 

image

If we chose, for example, Gmail app we will get:

image



How to implement ‘Tell a friend’ action Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment