从Android应用程序打开Twitter应用程序用户资料

17

我能启动 Twitter 应用(如果它在手机上存在的话),但我找不到如何自动显示特定用户资料的方法。

对于 Google+,类似这样的方法是有效的:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus", "com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", "USER_ID");

这里是 Facebook 的做法:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/USER_ID"));
startActivity(intent);

当我启动Twitter应用时,应该有同样的解决方案吗?

Intent intent = getPackageManager().getLaunchIntentForPackage("com.twitter.android");
intent.putExtra("WHAT_TO_PUT_HERE?", "USER_ID");
startActivity(intent);
3个回答

35
try {
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name)));
}catch (Exception e) {
   startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
}

新的 Twitter 应用程序可以正常工作。@Baptiste Costa 提供的解决方案对我没有用。


已确认; 但是:getPackageManager().getPackageInfo("com.twitter.android", 0); + 在NameNotFoundException上使用try/catch仍然有用,以便回退到Web应用程序(我的用例是将意图传递给通知,因此我需要提前知道什么会起作用-这是最直接的选项)。 - vincent
这个应该带还是不带 @ - Jonas Borggren
@JonasB 这个没有 '@' 符号。 - Chrishan

17
try
{
    // Check if the Twitter app is installed on the phone.
    getPackageManager().getPackageInfo("com.twitter.android", 0);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName("com.twitter.android", "com.twitter.android.ProfileActivity");
    // Don't forget to put the "L" at the end of the id.
    intent.putExtra("user_id", 01234567L);
    startActivity(intent);
}
catch (NameNotFoundException e)
{
    // If Twitter app is not installed, start browser.
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/AndroTesteur")));
}

这段代码在模拟器上运行正常,但是在我的设备上需要将user_id更改为screen_name,并且在intentTW.putExtra("screen_name", twitterProfile)之后安装Twitter应用程序才能正常工作。 - 735

2

这是一个很老的问题,我想为它提供一些解答。我通常使用用户ID或用户名(如果ID无效)进行操作。

Intent intent;
try {
    // Check if the Twitter app is installed on the phone.
    context.getPackageManager().getPackageInfo("com.twitter.android", 0);

    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=813679215195410432"));
} catch (Exception e) {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/LynxMods"));
}
context.startActivity(intent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接