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


Asked by: Julian319 | Posted: 20-01-2022






Answer 1

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

Answered by: Alfred170 | Posted: 21-02-2022



Answer 2

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

Answered by: Lily511 | Posted: 21-02-2022



Answer 3

This question is a bit older, but since I have just come across a similar problem, it may also still be of interest to others. First, as mentioned by Peter, create your intent:

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

"application/twitter" is in fact a known content type, see here. Now, when you try to start an activity with this intent, it will show all sorts of apps that are not really Twitter clients, but want a piece of the action. As already mentioned in a couple of the "why do you even want to do that?" sort of answers, some users may find that useful. On the other hand, if I have a button in my app that says "Tweet this!", the user would very much expect this to bring up a Twitter client.

Which means that instead of just launching an activity, we need to filter out the ones that are appropriate:

PackageManager pm = getPackageManager();
List<ResolveInfo> lract 
= pm.queryIntentActivities(tweetIntent,
    PackageManager.MATCH_DEFAULT_ONLY);

boolean resolved = false;

for(ResolveInfo ri: lract)
{
    if(ri.activityInfo.name.endsWith(".SendTweet"))
    {
        tweetIntent.setClassName(ri.activityInfo.packageName,
                        ri.activityInfo.name);
        resolved = true;
        break;
    }
}

You would need to experiment a bit with the different providers, but if the name ends in ".SendTweet" you are pretty safe (this is the activity name in Twidroyd). You can also check your debugger for package names you want to use and adjust the string comparison accordingly (i.e. Twidroyd uses "com.twidroid.*").

In this simple example we just pick the first matching activity that we find. This brings up the Twitter client directly, without the user having to make any choices. If there are no proper Twitter clients, we revert to the standard activity chooser:

startActivity(resolved ? tweetIntent :
    Intent.createChooser(tweetIntent, "Choose one"));

You could expand the code and take into account the case that there is more than one Twitter client, when you may want to create your own chooser dialog from all the activity names you find.

Answered by: Eric485 | Posted: 21-02-2022



Answer 4

It is entirely possible your users will only ever, now and forever, only want to post to Twitter.

I would think that it is more likely that your users want to send information to people, and Twitter is one possibility. But, they might also want to send a text message, or an email, etc.

In that case, use ACTION_SEND, as described here. Twidroid, notably, supports ACTION_SEND, so it will appear in the list of available delivery mechanisms.

Answered by: Patrick735 | Posted: 21-02-2022



Answer 5

These answers are all overly complex.

If you just do a normal url Intent that does to Twitter.com, you'll get this screen:

enter image description here

which gives you the option of going to the website if you have no Twitter apps installed.

String url = "https://twitter.com/intent/tweet?source=webclient&text=TWEET+THIS!";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Answered by: Freddie962 | Posted: 21-02-2022



Answer 6

Either

  • You start an activity with an Intent with action Intent.ACTION_SEND and the text/plain MIME type. You'll have all applications that support sending text. That should be any twitter client, as well as Gmail, dropbox, etc.
  • Or, you try to look up for the specific action of every client you are aware of, like "com.twitter.android.PostActivity" for the official client. That will point to this client, and that is unlikely to be a complete list.
  • Or, you start with the second point, and fall back on the first...

Answered by: Walter298 | Posted: 21-02-2022



Answer 7

Nope. The intent type is something like image/png or application/pdf, i.e. a file type, and with createChooser you're basically asking which apps can open this file type.

Now, there's no such thing as an application/twitter file that can be opened, so that won't work. I'm not aware of any other way you can achieve what you want either.

Answered by: Alfred640 | Posted: 21-02-2022



Answer 8

From http://twidroid.com/plugins/

Twidroid’s ACTION_SEND intent

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample message via Public Intent"); 
sendIntent.setType("application/twitter");   
startActivity(Intent.createChooser(sendIntent, null)); 

Answered by: Gianna101 | Posted: 21-02-2022



Answer 9

I used "billynomates" answer and was able to use hashtags by using the "URLEncoder.encode(, "UTF-8")" function. The hash tags showed up just fine.

String originalMessage = "some message #MESSAGE";

String originalMessageEscaped = null;
try {
   originalMessageEscaped = String.format(
    "https://twitter.com/intent/tweet?source=webclient&text=%s",
    URLEncoder.encode(originalMessage, "UTF-8"));
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

if(originalMessageEscaped != null) {
   Intent i = new Intent(Intent.ACTION_VIEW);
   i.setData(Uri.parse(originalMessageEscaped));
   startActivity(i);
}
else {
   // Some Error
}

Answered by: Julian333 | Posted: 21-02-2022



Similar questions

android application memory leaks

I am using kind of results search engine, problem is to remember the searching criteria i made singleton static class which keeps only one instance. In my application there are lots of class level private variables, lots of public static variables, a big util class which contains only static final methods. problem is my application get crash any time any where any spot and interesting thing is crash code a...


Should a legacy Android application be rebuilt using SDK 2.1?

I have an Android application that uses the well known Strategies for Legacy Applications. It is build with the Android SDK 2.0 with manifest settings minSdkVersion="3" (API 1.5) and targetSdkVersion="5" (2.0). Question1: Since maxSdkVersion is not specified, the application should be...


Android 1.6 SMS (older application code)

I have HTC Tattoo with Android 1.6. I have maed a Java program for SMS sending. I got the source on the Internet, I think, versions before 1.6. The problem is: SMSs are sent twice. What is a possible cause for this problem? If possible, please simply post sample code what works OK.


How to close Android application?

I want to close my application, so that it no longer runs in the background. How to do that? Is this good practice on Android platform? If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.


google play - Android Market Application Updates

Closed. This question is off-topic. It is not curre...


android - How come MapView shows less then Map application?

I have a com.google.android.maps.MapView in my application. When using the normal view I do not get any data in Jerusalem and its surroundings. When scrolling to the same location with the Maps app or the maps.google.com webpage the place is rich with mapdata. Is there a (known) difference between the Map API view and the Google view of mapdata that I am unaware of?


android - Max thread number for one application?

I wanna know about max thread number for one application. You know Thread.activeCount() returns the number of active Threads in the running Thread's group and its subgroups. If I can know the max number of threads to create in current activity, I can limit active threads. I am using thread for http connection and catching Http response. Thanks in advance.


How to delete a file from an Android application

How can I delete a file from an Android application? Can I do it the same way I would for deleting a file in Java?


How to match android application with droid mobile?

i have developed the android application screen resolution is 320x480 Px but i want to run same application without any code modification with Droid Mobile(480x854 px). i have installed android application with droid mobile but it's displaying only half of the page in droid mobile( i am using the android 2.0 SDK device). is there any way to resolve this kind of issues? Regards, Jeyavel N


Android Application looks like iphone

I came across this app and I wonder how was it developed on android (Cause it looks so much like an iphone app), see this screenshot. Anyone have any idea how to accomplish that type of look on Android?


android application memory leaks

I am using kind of results search engine, problem is to remember the searching criteria i made singleton static class which keeps only one instance. In my application there are lots of class level private variables, lots of public static variables, a big util class which contains only static final methods. problem is my application get crash any time any where any spot and interesting thing is crash code a...


Android Application data should not be released by android OS

public class MYApplication extends Application { String property; setter getter } does above code make sure property will not be collectied by android OS if not used for a long period of time.


Android Email application - Is it possible to get all the details which are configured in Email application

Is there any way to get in the code: Email id Password SMTP host SMTP port which the user used to setup his Email account with the Android built in Email application Required because, I am written a MailSender class using JavaMail API there I need to send the mail using the details what user configured in Built in Android Email application.


webserver - Retrieve data from a web server for android application

I am developing an application that needs to retrieve some data from a web server. But I have no idea how can this be possible?


Should a legacy Android application be rebuilt using SDK 2.1?

I have an Android application that uses the well known Strategies for Legacy Applications. It is build with the Android SDK 2.0 with manifest settings minSdkVersion="3" (API 1.5) and targetSdkVersion="5" (2.0). Question1: Since maxSdkVersion is not specified, the application should be...


android - Intent to pick an installed application slow in my activity, why?

If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications. I needed this same functionality in my application so I copied the intent from the launcher source: Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pic...


web services - How to store data accessible to both a web site and android application?

I’m developing an Android and web application that will function as a service (use the same data). My question is how should the data be stored to allow for both the web and the android application to have access to the same set of data? Should the android application connect to the sites MySQL server to store/access data? If so how do I allow someone to access the data when they are not in ...


Android 1.6 SMS (older application code)

I have HTC Tattoo with Android 1.6. I have maed a Java program for SMS sending. I got the source on the Internet, I think, versions before 1.6. The problem is: SMSs are sent twice. What is a possible cause for this problem? If possible, please simply post sample code what works OK.


How to close Android application?

I want to close my application, so that it no longer runs in the background. How to do that? Is this good practice on Android platform? If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.


listview - android application failing upon user scrolling - no 'obvious' error

I'm having a strange problem with Android 1.6 I have an application that has a ListView (found in Activity #1). The user can click on one of the rows in the ListView and is taken to a 2nd Activity (Activity # 2). User can then use the back button to go back to the ListView. The problem is that if the user scrolls down the list after returning from Activity # 2 the application crashes, bu...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top