Skip to content

Android: Improving Interfacing with twitter applications

by mat on January 16th, 2010

This post builds upon the ideas shown in this post to create a complete function that will do the follow:

  • Accept a string input for the message
  • Try to open twidroidpro to send this tweet
  • If failed, try to open twidroid to send this tweet
  • If failed, bring up a list of applications that can send this information
  • Return a boolean indicating success of displaying the intent

Code

public boolean intentTwitter(String message){
 // Boolean to show if we succeeded or not
 // we assume we did until proven otherwise.
 boolean success=true;

 //Try twidroidpro first
 Intent intent = new Intent("com.twidroidpro.SendTweet");
 intent.putExtra("com.twidroidpro.extra.MESSAGE", message); 
 intent.setType("application/twitter"); 
   try{
    startActivityForResult(intent, 1);
   }
   catch(ActivityNotFoundException e){
    success=false;
   }

 // Then twidroid if we failed
 if (!success){
  success=true;
  intent = new Intent("com.twidroid.SendTweet");
  intent.putExtra("com.twidroid.extra.MESSAGE", message); 
  intent.setType("application/twitter"); 
    try{
     startActivityForResult(intent, 1);
    }
    catch(ActivityNotFoundException e){
     success=false;
    }
 }
   
   
 //Then send general intent if we failed again
 if (!success){
  success=true;
   try {
    intent = new Intent(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_TEXT, d); 
    intent.setType("application/twitter"); 
    startActivity(Intent.createChooser(intent, null));
   } catch (ActivityNotFoundException e) {
    success=false;
   }
 }
// return indicating if we were successful in bringing up an intent
// of some description
return success;
}

Example
Below is a screenshot from the wordcube (available on market) application using this function to make the application interact with twitter.

twidroid intent screenshot

twidroid intent called from wordcube

What’s next?
In a future post I will be demonstrating how to interact with the android market, this can be use in conjunction with something similar to above to try and run a program and if the program is not installed the user can be taken to the market to download a program.

4 Comments
  1. Note that there is no such MIME type as “application/twitter”.

    You might also consider using PackageManager to detect whether an Intent will be used rather than causing exceptions to be raised and caught.

  2. I believe that applications can be set to listen to whatever MIME type they choose, and I know for twidroid that “application/twitter” works. However it may be that other twitter applications do not, and so it might be more of a well-rounded solution to send “text/plain” so that any program that accepts text can respond and the user can choose from a list.

    What do you think? Also I will have a look into PackageManager to save using try/catch, thanks.

    Ref:
    http://twidroid.com/plugins/

  3. This (see link below) provides the basis for a more elegant solution, which can be used to create a chooser displaying only items that match providing they are installed, or provide an option for user to goto market and install them. I will try and update wordcube to use this method, and will post a new function once it is created.

    http://developer.android.com/resources/articles/can-i-use-this-intent.html

Trackbacks & Pingbacks

  1. uberVU - social comments

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS