From my testing, I could not find a good way to do this and instead resorted to a solution that is not necessarily βbest practiceβ. It only works with the official Twitter app and not with others. This solution will not succeed if the official application changes the internal API. Therefore, please use this solution with caution and be aware of its limitations.
This code is not written correctly, but it works. My advice is to change it so as not to use so many resources.
The code checks if the Twitter application is installed. If so, the Twitter application launches; otherwise, the browser starts.
Twitter has the name Twitter (also the name screen_name) and twitter id: they do not match.
//Checking If the app is installed, according to the package name Intent intent = new Intent(); intent.setType("text/plain"); intent.setAction(Intent.ACTION_SEND); final PackageManager packageManager = getPackageManager(); List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo resolveInfo : list) { String packageName = resolveInfo.activityInfo.packageName; //In case that the app is installed, lunch it. if (packageName != null && packageName.equals("com.twitter.android")) { try { String formattedTwitterAddress = "twitter://user/" ; Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress)); long twitterId = <Here is the place for the twitter id> browseTwitter.putExtra("user_id", twitterId); startActivity(browseTwitter); return; } catch (Exception e) { } } } //If it gets here it means that the twitter app is not installed. Therefor, lunch the browser. try { String twitterName = <Put the twitter name here> String formattedTwitterAddress = "http://twitter.com/" + twitterName; Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress)); startActivity(browseTwitter); } catch (Exception e) { }
source share