Rafael Sanches

June 18, 2010

Using native twitter app Intent to share on android

Filed under: android — mufumbo @ 6:36 pm

I have been googling to find the Twitter native app intent that enables to share through it directly, but I couldn’t find any.

ATTENTION: This is just a hack. It’s better to just open the android popup with the Intent.ACTION_SEND intent.

I didn’t had time to test other ways, but the only way to launch the twitter PostActivity directly was to get the ActivityInfo instance of it. Please, give me feedback if you know a better way.

What this code does is to query all activities that match with the Intent.ACTION_SEND and then it searches for the “com.twitter.android.PostActivity” intent.

try{
    intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, message);
    intent.setType("text/plain");
    final PackageManager pm = context.getPackageManager();
    final List activityList = pm.queryIntentActivities(intent, 0);
        int len =  activityList.size();
    for (int i = 0; i < len; i++) {
        final ResolveInfo app = activityList.get(i);
        if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
            final ActivityInfo activity=app.activityInfo;
            final ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name);
            intent=new Intent(Intent.ACTION_SEND);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            intent.setComponent(name);
            intent.putExtra(Intent.EXTRA_TEXT, message);
            context.startActivity(intent);
            break;
        }
    }
}
catch(final ActivityNotFoundException e) {
    Log.i("mufumbo", "no twitter native",e );
}
About these ads

11 Comments »

  1. Thanks, this worked nicely.

    Comment by Jason Hudgins — July 21, 2010 @ 6:47 pm

  2. Thanks a lot, this is a much better option than twitdroyd! I do have one problem, however. When I log onto twitter without using my app and access the feed, then once I launch my app with the intent to post something, the feed is the first screen to display – not the tweet screen. Do you have a way around this?

    Thanks

    Comment by Phil Brown — March 24, 2011 @ 8:07 pm

    • i don’t have a way around it. I think this has to be handled by the twitter client.

      Actually it may work using some intent parameters like intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      Can you try that and let me know if it works? :)

      Comment by mufumbo — March 24, 2011 @ 8:59 pm

      • Didn’t work for me. It still sometimes just goes to stream instead of tweet screen.

        Comment by ed — August 2, 2011 @ 6:19 pm

      • that happens when the twitter app is in the foreground in another screen. This technique only works when the twitter app is not opened.

        i hope they update their app soon

        Comment by mufumbo — August 3, 2011 @ 2:27 am

  3. hi
    In my application I am clicking one share image button and four options facebook, twitter, mail and messaging are showing through alert Dialog box. I want to share Text through first option(first Facebook). How to do that.

    here my code:
    final CharSequence[] items = {“Twitter”, “Facebook”, “E-Mail”, “Messaging”};
    AlertDialog.Builder builder = new AlertDialog.Builder(ProgramInfoActivity.this);
    builder.setTitle(“Share the Program”);
    builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
    if( item == 0){

    }
    else if (item == 1){

    }
    else if (item == 2){
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, “This is a sample message via Public Intent”);
    sendIntent.setType(“application/twitter”);
    startActivity(Intent.createChooser(sendIntent, null));
    System.out.println(“+++Item 2 Calling+++”);
    }
    else if (item == 3){

    }
    }});
    AlertDialog alert = builder.create();
    alert.show();
    Can you help me for this

    Comment by Raj — August 17, 2011 @ 7:12 am

    • have you tried the code that i’ve pasted for this?

      Comment by mufumbo — August 17, 2011 @ 7:31 am

      • hi
        can you explain your code. How to use the code because i am very new android past two days struggling this. I want only above four options and with working functionality. how to do please help me for this.

        Comment by Raj — August 17, 2011 @ 8:37 am

  4. hi

    I wnt to share link in facebook n twitter app
    But above code is not working for me

    I have seen all replies
    But it does not work

    How to this task?

    Comment by dps — December 9, 2011 @ 2:22 pm

  5. [...] a combination of blog posts and StackOverflow questions I came across the following solution for posting directly to the [...]

    Pingback by Learning Android: Sharing with Twitter/the ‘share via’ dialog at Mark Needham — December 29, 2011 @ 10:43 pm

  6. Thanx alot its work fine.

    Comment by mahesh — September 19, 2013 @ 12:10 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Theme: WordPress Classic. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 406 other followers

%d bloggers like this: