Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a "Tweet this button" to share information only on Twitter is not so easy

For my application, I display a twitterfeed inside the main screen. What is cool is that people can follow the twitter information about the train company of my country without having a Twitter account.

enter image description here

What I would like to do is reacting when user click on an item and then allow him to reply/RT. If the user does not have a twitter application installed, then nothing should happen ( maybe a toast)

I tried this action:

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

But seem to open also email, Gmail and other useless applications.

Is there a way to filter that list and keep only Twitter applications ( Twitter, Twydroid, Plume, Twicca, etc..)

Thank a lot for any suggestion.

Edit: My current workaround, that I don't like AT ALL:

public Intent findTwitterClient() {
        final String[] twitterApps = {
                // package // name
                "com.twitter.android", // official
                "com.levelup.touiteur", // Plume 
                "com.twidroid", // twidroyd
                "com.handmark.tweetcaster", //
                "com.thedeck.android" //  };
        Intent tweetIntent = new Intent();
        tweetIntent.setType("text/plain");
        final PackageManager packageManager = getPackageManager();
        List<resolveinfo> list = packageManager.queryIntentActivities(
                tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);

        for (int i = 0; i <twitterApps.length; i++) {
            for (ResolveInfo resolveInfo : list) {
                String p = resolveInfo.activityInfo.packageName;
                if (p != null && p.startsWith(twitterApps[i])) {
                    tweetIntent.setPackage(p);
                    return tweetIntent;
                }
            }
        }
        return null;
    }
like image 279
Waza_Be Avatar asked Nov 13 '22 19:11

Waza_Be


1 Answers

There is no official twitter API in android, I highly suggest you implement your own with something like twitter4j.

http://twitter4j.org/en/index.html

You should know the user ID and tweet ID, etc, etc. when clicked, then you can use twitter4j like this:

Twitter twitter = new TwitterFactory().getInstance();
twitter.updateStatus(new StatusUpdate("@USERNAME").inReplyToStatusId(STATUSID)); 

I'm not sure if you're already using twitter4j or not, if you aren't i highly suggest you do as querying for tweets is very easy.

You also need authentication within your app if you use twitter4j, it's fairly simple but i won't get into it since you'll need to popup an authentication window, etc. There's an example at http://twitter4j.org/en/code-examples.html


If you don't want to go the twitter4j route, unfortunately your way is the only way i know of. Simply use the package manager to check if they exist and notify the user otherwise.

like image 153
bbedward Avatar answered May 26 '23 20:05

bbedward