从其他应用程序打开 Twitter 应用程序中的页面 - Android

问题描述 投票:0回答:8

我正在寻找某种方法来启动 Twitter 应用程序并从我的应用程序中打开指定的页面,而不需要 webview。 我在这里找到了 Facebook 的解决方案: 在指定的个人资料页面上打开Facebook应用程序

我需要类似的东西。

[编辑]

我刚刚找到了解决方案:

try {
    Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("twitter://user?screen_name=[user_name]"));
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    startActivity(new Intent(Intent.ACTION_VIEW,
    Uri.parse("https://twitter.com/#!/[user_name]"))); 
}
android twitter
8个回答
49
投票

基于 fg.radigales 的回答,如果可能的话,这就是我用来启动应用程序的方法,但否则返回到浏览器:

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?user_id=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/PROFILENAME"));
}
this.startActivity(intent);

更新

添加了

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
以修复 Twitter 在我的应用程序中打开而不是作为新活动打开的问题。


43
投票

这对我有用:

twitter://user?user_id=id_num


10
投票

使用 Android 在 2 个步骤中从其他应用程序打开 Twitter 应用程序页面:

1.只需粘贴以下代码(单击按钮或任何您需要的地方)

Intent intent = null;
try{
   // Get Twitter app
   this.getPackageManager().getPackageInfo("com.twitter.android", 0);
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch () {
   // If no Twitter app found, open on browser
   intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERNAME"));
}

2.

intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USER_ID"));

要获取 USER_ID,只需写入用户名 https://tweeterid.com/ 并在其中获取 Twitter 用户 ID

参考https://solutionspirit.com/open-page-twitter-application-android/


4
投票

我的答案建立在 fg.radigales 和 Harry 广泛接受的答案之上。 如果用户安装了 Twitter 但已禁用(例如通过使用应用程序隔离),则此方法将不起作用。 Twitter 应用程序的意图将被选择,但它将无法处理它,因为它已被禁用。

代替:

getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));

您可以使用以下信息来决定要做什么:

PackageInfo info = getPackageManager().getPackageInfo("com.twitter.android", 0);
if(info.applicationInfo.enabled)
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=2343965036"));
else
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/wrkoutapp"));

2
投票
try {
    Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("twitter://user?screen_name=[user_name]"));
    startActivity(intent);
} catch (Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW,
    Uri.parse("https://twitter.com/#!/[user_name]"))); 
}

此答案作为 edit 发布到问题 Open page in Twitter app from other app - Android by OP jbc25 under CC BY-SA 3.0。


1
投票

试试这个代码片段。它会对你有帮助。

//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) 
        {

        }

1
投票

对我来说,如果你有 Twitter 应用程序或进入网络浏览器,它会打开 Twitter 应用程序:

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
                    startActivity(intent);

0
投票

要将 Twitter 用户名转换为 ID 或将 ID 转换为用户名:

tweeterid.com 已弃用
tweeter-id.com 运行良好

© www.soinside.com 2019 - 2024. All rights reserved.