Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start twitter app [duplicate]

Possible Duplicate:
Android Intent for Twitter application

I want to go to follow page from my android app and i want this to be done by starting the native twitter app.

How can i do this and also if a twitter client is not there in user's mobile it should go in mobile web site of twitter.

like image 471
AutoMEta Avatar asked Apr 08 '11 12:04

AutoMEta


3 Answers

To check if an Intent exists try this:

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

(source)

For twitter check this snippet:

Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "Test tweet");
tweetIntent.setType("application/twitter");

(source)

like image 149
Mark Mooibroek Avatar answered Oct 23 '22 13:10

Mark Mooibroek


From my testing, I couldn't find a nice way to do this, and instead resorted to a solution that is not necessarily 'best practice'. It works only with the official twitter app and not with the others. This solution will fail if the official app changes its internal API. Therefore, please use this solution with caution and know its limitations.

This code is not written in a good way, but it does work. My advice is to change it to not use so many resources.

The code checks to see whether the Twitter app is installed. If so, the Twitter app is launched; otherwise, the browser is launched.

Twitter has twitter name (also named screen_name) and twitter id: they are not the same.

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

        }
like image 5
nheimann1 Avatar answered Oct 23 '22 12:10

nheimann1


Since the last version of Twitter app 3.0.0 at the Dec 9 2011, the official twitter app support the regular intent mechanism. All you need to do is to use the regular intent for the browser, in case that it will be a twitter valid address the official app is registered as one of the resolver of this intent.

... just copy and paste the comment of goBeepit dev because works for me, the android ask you for open in browser or twitter app, what I've use its just this

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/norman784"));
startActivity(intent);
like image 5
norman784 Avatar answered Oct 23 '22 14:10

norman784