I use this code to start posting to twitter using the official Twitter app:
Intent twitterIntent = new Intent(Intent.ACTION_VIEW); //Intent.ACTION_VIEW twitterIntent.setAction("android.intent.action.SEND"); twitterIntent.setFlags(0x3000000); twitterIntent.setType("text/plain"); twitterIntent.setClassName("com.twitter.android", "com.twitter.android.PostActivity"); twitterIntent.putExtra(Intent.EXTRA_TEXT, ("Random post")); startActivity(twitterIntent);
I also check if this intent is available before using it with:
public static boolean canReceiveIntent (Intent intent, Context c) { PackageManager packageManager = c.getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); boolean isIntentSafe = activities.size() > 0; return isIntentSafe; }
It works fine, but the Twitter application remains open after the tweet is posted (via the channel). I also tried using startActivityForresult (), but it fails with an error:
java.lang.RuntimeException: android.util.AndroidRuntimeException: FORWARD_RESULT_FLAG used while also requesting a result
How to return to my application after posting a tweet?
source share