Android launch Twitter intent
Slightly corrected (thanks to Taranfx) an user's feed intent (change user_id=>screen_name):
public static void startTwitter(Context context) {
Intent intent = null;
try {
// get the Twitter app if possible
context.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(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 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/<place_user_name_here>"));
}
startActivity(intent);
}
Typically for launching a user's feed
Intent intent = null;
try {
// get the Twitter app if possible
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(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 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERID_OR_PROFILENAME"));
}
this.startActivity(intent);
For Post Intent
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = 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();
}