Mark is a graph advocate and field engineer for Neo Technology, the company behind the Neo4j graph database. As a field engineer, Mark helps customers embrace graph data and Neo4j building sophisticated solutions to challenging data problems. When he's not with customers Mark is a developer on Neo4j and writes his experiences of being a graphista on a popular blog at http://markhneedham.com/blog. He tweets at @markhneedham. Mark is a DZone MVB and is not an employee of DZone and has posted 548 posts at DZone. You can read more from them at their website. View Full User Profile

Learning Android: Sharing with Twitter/the 'share via' dialog

01.12.2012
| 9106 views |
  • submit to reddit

One thing I wanted to do in the little application I’m working on was send data to other apps on my phone using the ‘share via’ dialog which I’ve seen used on the Twitter app.

In this case I wanted to send a link and its title to twitter and came across a StackOverflow post which explained how to do so.

To keep it simple I added a button to the view and then shared the data via the on click event on that button:

Button button = createButton();
button.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Stupid Scanner tricks - http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html");
    v.getContext().startActivity(Intent.createChooser(shareIntent, "Share via"));
  }
});

In this case if I choose the Twitter app from the drop down list that appears it will create a new tweet with the value of ‘android.content.Intent.EXTRA_TEXT’ as its body.

If we needed to pass a subject to the other app then we could set that using ‘android.content.Intent.EXTRA_SUBJECT’ but in this case it’s unnecessary.

From what I understand so far only apps which can handle the ‘text/plain’ format will show up in the drop down list but that seems to be pretty much every app on my phone.

This works reasonably well but I wanted to see if it was possible to share directly with the Twitter app rather than having to choose from a selection of apps.

Via a combination of blog posts and StackOverflow questions I came across the following solution for posting directly to the Twitter app:

Button button = createButton();
button.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Some text");
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Stupid Scanner tricks - http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html");
 
    final PackageManager pm = v.getContext().getPackageManager();
    final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {
      if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
        final ActivityInfo activity = app.activityInfo;
        final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
        shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        shareIntent.setComponent(name);
        v.getContext().startActivity(shareIntent);
        break;
      }
    }
  }
}):;

It does depend on the Twitter app being installed on the phone to work but since the app is just for me I think it’s ok for the moment.

 

From http://www.markhneedham.com/blog/2011/12/29/learning-android-sharing-with-twitterthe-share-via-dialog

Published at DZone with permission of Mark Needham, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Afandi Merathi replied on Sun, 2012/03/18 - 7:09am

its working fine, but sometime i share one after another again and again, then postactivity not opened 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.