Ankush
Ankush

Reputation: 6927

Tweeting by using intent from android app

I'm using the following code to tweet something from another app:

try{
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, "It's a Tweet!" + "#MyApp");
        intent.setType("text/plain");
        final PackageManager pm = getPackageManager();
        final List<?> activityList = pm.queryIntentActivities(intent, 0);
        int len =  activityList.size();
        for (int i = 0; i < len; i++) {
            final ResolveInfo app = (ResolveInfo) activityList.get(i);
            if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
                final ActivityInfo activity=app.activityInfo;
                final ComponentName x=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(x);
                intent.putExtra(Intent.EXTRA_TEXT, "blah blah" );
                startActivity(intent);
                break;
            }
        }
    } catch(final ActivityNotFoundException e) {
        Log.i("Twitter intent", "no twitter native", e );
    }

The above code works perfectly if the twitter app is installed on the phone but is not open. But if I was using twitter before using the above activity, then I just simply get transferred to the same point where I last left twitter and the text that I wanted to tweet is not posted. What should I do to correct this?

Also, if twitter is not installed on my phone, then nothing happens. On pressing the button that starts the intent above nothing happens but shouldn't I actually be getting the message - no twitter native. What should be done to get the message??

Upvotes: 3

Views: 2583

Answers (2)

desgraci
desgraci

Reputation: 1188

Simple remove Intent.FLAG_ACTIVITY_NEW_TASK for Intent.FLAG_ACTIVITY_CLEAR_TASK, so the task where the twitter is running gets re-started.

Your flags should look like this now:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

Hope this helps, and remember to accept the answer if correct :)

Upvotes: 1

Anup Cowkur
Anup Cowkur

Reputation: 20563

I think you should use setClassName instead of setComponent.

  intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");

But as a helpful side note, There are several options for you to integrate with twitter:

1)Use their API (maybe use a library such as twitter4j), or

2)Find a way to integrate with the Twitter app, like you are doing right now. This is not a good idea because many people use other apps such as Plume, TweetDeck and so on and your app wont work if they dont have twitter installed.

3)you can just open up http://twitter.com/?status= using a simple intent. This can be done like:

  Intent i = new Intent(Intent.ACTION_VIEW);
  i.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));
  startActivity(i);

You can also combine 2 and 3. You can try a few known twitter apps and if they are absent, call the browser.

Also note that it might be possible for method 3 to actually launch an app instead of the browser (or at least give the user a choice between the two), if the app handles the correct intents.

In my opinion, Method 1 is best since it only depends on your app ( well, except for the library) and you don't have to integrate with third party apps that the user may or may not have.

Upvotes: 2

Related Questions