Skip to content Skip to sidebar Skip to footer

Android Launch Twitter Intent

I used below code for launching Twitter through intent but it's not working. I have twitter app installed on my phone. Intent shareIntent = new Intent(android.content.Intent.ACTION

Solution 1:

Typically for launching a user's feed

Intentintent=null;
try {
    // get the Twitter app if possiblethis.getPackageManager().getPackageInfo("com.twitter.android", 0);
    intent = newIntent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=USERID"));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
    // no Twitter app, revert to browser
    intent = newIntent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERID_OR_PROFILENAME"));
}
this.startActivity(intent);

For Post Intent

IntenttweetIntent=newIntent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");

PackageManagerpackManager= getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent,  PackageManager.MATCH_DEFAULT_ONLY);

booleanresolved=false;
for(ResolveInfo resolveInfo: resolvedInfoList){
    if(resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")){
        tweetIntent.setClassName(
            resolveInfo.activityInfo.packageName, 
            resolveInfo.activityInfo.name );
        resolved = true;
        break;
    }
}
if(resolved){
    startActivity(tweetIntent);
}else{
    Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}

Solution 2:

Slightly corrected (thanks to Taranfx) an user's feed intent (change user_id=>screen_name):

publicstaticvoidstartTwitter(Context context) {

    Intentintent=null;
    try {
        // get the Twitter app if possible
        context.getPackageManager().getPackageInfo("com.twitter.android", 0);
        intent = newIntent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=<place_user_name_here>"));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    } catch (Exception e) {
        // no Twitter app, revert to browser
        intent = newIntent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/<place_user_name_here>"));
    }
    startActivity(intent);
}

Solution 3:

I was using the com.twitter.android.composer.ComposerActivity to post text and images since 2016. But started to receive crash reports from users some time ago:

Fatal Exception: android.content.ActivityNotFoundException Unable to find explicit activity class {com.twitter.android/com.twitter.android.composer.ComposerActivity}; have you declared this activity in your AndroidManifest.xml?

The issue was caused by renaming com.twitter.android.composer.ComposerActivity to com.twitter.composer.ComposerActivity inside Twitter app.

The issue was resolved since I changed activity name to com.twitter.composer.ComposerActivity.

And I use the following code to post images with text to Twitter:

ShareCompat.IntentBuilder.from(activity)
    .setText(getTextToShare())
    .setStream(getImageUriToShare())
    .setType("image/png")
    .getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    .setClassName("com.twitter.android", "com.twitter.composer.ComposerActivity");

Post a Comment for "Android Launch Twitter Intent"