Tweeting using twitter for android intent

Well am working on this application where I want to be able to posts tweets using the native android application.  Here is the how to.

launch the twitter PostActivity directly by getting the ActivityInfo instance of it. This is done by scanning through the activities returned by the Intent.ACTION_SEND and looking for “com.twitter.android.PostActivity”.

This is a not a clean way of doing it since we are searching for a particular hard coded activity. The best way is to just let the user select from the dialog box returned by Intent.ACTION_SEND.

Context context = getApplication();
String message = "Testing integration into native twitter android app :)";
message = "It works!";
try {
Intent 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 = (ResolveInfo) 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) {
Toast.makeText(this, "Damn! no suitable Twitter apps found.",
Toast.LENGTH_SHORT) .show();
}


Happy Coding.

About these ads

One thought on “Tweeting using twitter for android intent

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