Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Intent for Twitter application

Is it possible to show a list of applications (with intent.createChooser) that only show me my twitter apps on my phone (so htc peep (htc hero) or twitdroid). I have tried it with intent.settype("application/twitter") but it doesnt find any apps for twitter and only shows my mail apps.

Thank you,

Wouter

like image 557
wouter88 Avatar asked Jan 16 '10 10:01

wouter88


People also ask

What is Twitter intent?

A Tweet Web Intent makes it easy for your site visitors to compose and post a Tweet to their audience from a link on your webpage. Sites may configure Tweet text and hashtags, pass a URL, and identify Twitter accounts related to the page.

What is intent in Android application?

An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases: Starting an activity. An Activity represents a single screen in an app.

How do you use Twitter intent tweets?

After “screen_name=” type in the users Twitter username in order to display the correct mini profile. Tweet: <a href=”http://twitter.com/intent/tweet”>Send your own message</a> This code pops up a window allowing the user to send out a normal tweet.

What is intent in android with example?

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc.


2 Answers

I'm posting this because I haven't seen a solution yet that does exactly what I want.

This primarily launches the official Twitter app, or if that is not installed, either brings up a "Complete action using..." dialog (like this) or directly launches a web browser.

For list of different parameters in the twitter.com URL, see the Tweet Button docs. Remember to URL encode the parameter values. (This code is specifically for tweeting a URL; if you don't want that, just leave out the url param.)

// Create intent using ACTION_VIEW and a normal Twitter url: String tweetUrl = String.format("https://twitter.com/intent/tweet?text=%s&url=%s",         urlEncode("Tweet text"),          urlEncode("https://www.google.fi/")); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(tweetUrl));  // Narrow down to official Twitter app, if available: List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0); for (ResolveInfo info : matches) {     if (info.activityInfo.packageName.toLowerCase().startsWith("com.twitter")) {         intent.setPackage(info.activityInfo.packageName);     } }  startActivity(intent); 

(URL encoding is cleaner if you have a little utility like this somewhere, e.g. "StringUtils".)

public static String urlEncode(String s) {     try {         return URLEncoder.encode(s, "UTF-8");     }     catch (UnsupportedEncodingException e) {         Log.wtf(TAG, "UTF-8 should always be supported", e);         throw new RuntimeException("URLEncoder.encode() failed for " + s);     } } 

For example, on my Nexus 7 device, this directly opens the official Twitter app:

enter image description here

If official Twitter app is not installed and user either selects Chrome or it opens automatically (as the only app which can handle the intent):

enter image description here

like image 73
Jonik Avatar answered Sep 21 '22 12:09

Jonik


The solutions posted before, allow you to post directly on your first twitter app. To show a list of twitters app (if there are more then one), you can custom your Intent.createChooser to show only the Itents you want.

The trick is add EXTRA_INITIAL_INTENTS to the default list, generated from the createChoose, and remove the others Intents from the list.

Look at this sample where I create a chooser that shows only my e-mails apps. In my case appears three mails: Gmail, YahooMail and the default Mail.

private void share(String nameApp, String imagePath) {     List<Intent> targetedShareIntents = new ArrayList<Intent>();     Intent share = new Intent(android.content.Intent.ACTION_SEND);     share.setType("image/jpeg");     List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);     if (!resInfo.isEmpty()){         for (ResolveInfo info : resInfo) {             Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);             targetedShare.setType("image/jpeg"); // put here your mime type              if (info.activityInfo.packageName.toLowerCase().contains(nameApp) ||                      info.activityInfo.name.toLowerCase().contains(nameApp)) {                 targetedShare.putExtra(Intent.EXTRA_TEXT,     "My body of post/email");                 targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );                 targetedShare.setPackage(info.activityInfo.packageName);                 targetedShareIntents.add(targetedShare);             }         }          Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");         chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));         startActivity(chooserIntent);     } } 

You can run like that: share("twi", "/sdcard/dcim/Camera/photo.jpg");

This was based on post: Custom filtering of intent chooser based on installed Android package name

like image 37
Derzu Avatar answered Sep 24 '22 12:09

Derzu