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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11105103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:01:12  来源:igfitidea点击:

Open page in Twitter app from other app - Android

androidtwitter

提问by jbc25

I was looking for some way to launch Twitter app and open a specified page from my application, without webview. I found the solution for Facebook here: Opening facebook app on specified profile page

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

I need something similar.

我需要类似的东西。

EDITI just found a solution:

编辑我刚刚找到了一个解决方案:

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]"))); 
}

采纳答案by fg.radigales

This worked for me: twitter://user?user_id=id_num

这对我有用: twitter://user?user_id=id_num

回答by Harry

Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:

根据 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);

UPDATE

更新

Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);to fix an issue where twitter was opening inside my app instead of as a new activity.

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

回答by Solution Spirit

Open page on Twitter app from other app using Android in 2 Steps:

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

1.Just paste the below code (on button click or anywhere you need)

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"));

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

To get USER_ID just write username http://gettwitterid.com/and get Twitter User ID in there

要获取 USER_ID 只需写入用户名http://gettwitterid.com/并在那里获取 Twitter 用户 ID

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

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

Hope it will Help :)

希望它会有所帮助:)

回答by igordc

My answer builds on top of the widely-accepted answers from fg.radigales and Harry. If the user has Twitter installed but disabled (for example by using App Quarantine), this method will not work. The intent for the Twitter app will be selected but it will not be able to process it as it is disabled.

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

Instead of:

代替:

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

You can use the following to decide what to do:

您可以使用以下内容来决定要做什么:

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"));

回答by Akilan

Just try this code snippet. It will help you.

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

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

        }

回答by Hussein Alghazal

For me this did the trick it opens Twitter app if you have it or goes to web browser:

对我来说,如果您拥有它或转到网络浏览器,它就可以打开 Twitter 应用程序:

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