Open page in Twitter app from other app - Android

前端 未结 6 1951
南旧
南旧 2020-12-22 20:54

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

相关标签:
6条回答
  • 2020-12-22 21:20

    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) 
            {
    
            }
    
    0 讨论(0)
  • 2020-12-22 21:26

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

     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/"+"USERID"));
                        startActivity(intent);
    
    0 讨论(0)
  • 2020-12-22 21:27

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

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

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

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

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

    Hope it will Help :)

    0 讨论(0)
  • 2020-12-22 21:32

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

    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.

    0 讨论(0)
  • 2020-12-22 21:39

    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.

    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"));
    
    0 讨论(0)
  • 2020-12-22 21:45

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

    0 讨论(0)
提交回复
热议问题