you can do this by finding the Twitter application from the intent of sharing, if it is installed, but if you canβt handle this condition and open your Twitter application page, as in my lower code, since I found that twitter4j is difficult to integrate and give results inconsistencies.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject"); shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, " text"; final PackageManager pm = v.getContext().getPackageManager(); final List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0); boolean twtchk=false; for (final ResolveInfo app : activityList) { if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) { final ActivityInfo activity = app.activityInfo; System.out.println("package==="+activity.applicationInfo.packageName); final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name); shareIntent.addCategory(Intent.CATEGORY_LAUNCHER); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); shareIntent.setComponent(name); v.getContext().startActivity(shareIntent); twtchk=true; break; } } if(!twtchk) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/yourapp")); startActivity(intent); }
source share