Skip to content

Posts tagged ‘twitter’

Android: Improving Interfacing with twitter applications

Jan 16 10
by mat

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.

Android: Interfacing with twitter applications

Jan 13 10
by mat

Below are some code snippets for sending messages to twitter from your application by utilizing a twitter application.

The following code will create a new intent(a request to android for something to happen) for the twidroid application and pass it the message we wish to send. It is important to set the type of the intent as it will fail without it (from at least android 1.5). It will then start the activity and we use a try/catch encase twidroid is not found.

	Intent intent = new Intent("com.twidroid.SendTweet");
	intent.putExtra("com.twidroid.extra.MESSAGE",
	"@stealthcopter Example tweet from android application");
	intent.setType("application/twitter"); 
	try {
		startActivity(intent);
	} 
	catch (ActivityNotFoundException e) {
		/* Handle Exception if Twidroid is not installed */
		Toast.makeText(this, "Twidroid not found.", Toast.LENGTH_SHORT).show();
	}

But what if someone has a different twitter application installed? this can be solved by offering the user a choice of application to open.

	Intent intent = new Intent(Intent.ACTION_SEND);
	intent.putExtra(Intent.EXTRA_TEXT,
	"@stealthcopter Example tweet from android application");
	intent.setType("application/twitter"); 
	try {
		startActivity(Intent.createChooser(intent, null));
	} 
	catch (ActivityNotFoundException e) {
		/* Handle Exception if no suitable apps installed */
		Toast.makeText(this, "No suitable apps found.", Toast.LENGTH_SHORT).show();
	}

By combining these two solutions we can make the choice dialog only pop up if the system cannot find the twidroid application. This can be then taken even further by asking the user if they wish to visit the market place to install a twitter application if none is found.

   new AlertDialog.Builder(WordCube.this)
   .setTitle("Get Twitter")
   .setMessage("No twitter Application not found. Goto market and install one now?")
   .setIcon(R.drawable.logo)
   .setNegativeButton(R.string.dialog_no, null)
                   .setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	intentMarket("market://search?q=twitter");
                }
            })
   .show();

Where intentMarket() is defined a to open up the market and search for the specified application passed to it as a string.

public void intentMarket(String url){
	Intent i = new Intent(Intent.ACTION_VIEW); 
	Uri u = Uri.parse(url); 
	i.setData(u); 
	try {
		startActivity(i);
	} catch (ActivityNotFoundException e) {
		toastMessage("Market not found.");
	}
}

Below shows two screenshots from the wordcube application. This application is avaliable for android, see here for more information and download link (or visit market on your android device).

twidroid intent screenshot

twidroid intent called from wordcube


choice box after intent

choice box for twitter applications

Update: This is now taken into a complete function in this post.