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 );
}

Blog at WordPress.com.

%d bloggers like this: