Skip to content

Posts tagged ‘eclipse’

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.

Programming Android Apps: SDK and Eclipse (ubuntu)

Jan 5 10
by mat

Android is a brilliant smart phone operating system, this is the start of a short series of guides for starting to program applications for it using the android SDK.

Android SDK
Download the android SDK

Once downloaded untar the SDK

tar xvzf android-sdk_r04-linux_86.tgz

The SDK is not complete as additional files need to be downloaded in order to compile for different versions of android. Open the SDK and AVG management application by moving into the SDK folder and running the following.

sh tools/android

In the avaliable packages select the android versions you wish to develop for, and begin downloading them. Should this fail please read the next section, otherwise skip ahead.

Failing to download
If you cannot download from the google website, goto settings and select “force https://… source to be fetched using http://” and click save and apply.

android force http

forcing SDK and AVD manager http instead of https for android

If this still does not work (as was the case for me) it is possible that for some reason a configuration file was not created for this program, this can be solved by creating it manually:

echo sdkman.force.http=true > ~/.android/androidtool.cfg

Creating Android Virtual Devices
You can create virtual android phones using the SDK and AVD manager, click the Virtual Device tab and select new. Enter a name for the device, and a size for the sd card and simply click create AVD.

android create avd

creation of an android virtual device

Once you’ve created you Virtual Device(s) it should look like the following:

android avd's

Android Virtual Devices

You can test these virtual devices and see how nicely the phones are emulated. This is much more useful once you begin writing applications.

Android virtual device

Android Virtual Device in action

Eclipse

I would highly recommend using eclipse as it, along with the android plugin, greatly simplifies production and testing of applications.

Download eclipse from the ubuntu repositories (or from the eclipse website)

sudo apt-get install eclipse

If you do not already have java installed then you will need to install it.

sudo apt-get install sun-java6-jdk sun-java6-jre

You will need to add the following line to your .bashrc in your home folder so that the android tools can be used in eclipse (and other programs).

export PATH=${PATH}:/home/user/android/sdk/tools

* replace /home/user/android/sdk with the path to where you downloaded the SDK

Installing the android plugin for eclipse
Google’s eclipse plugin install guide.

In eclipse goto help then Install new software and then add the google plugin url

https://dl-ssl.google.com/android/eclipse/

Install software

Install new software in eclipse

Then install Android DDMS and Android Development Tools.

Should you receive errors (like I did) relating to a missing package you will need to add the eclipse repository and install the missing packages.

http://download.eclipse.org/releases/galileo

You should then have a fully working eclipse with android plugin.

Eclipse main window

Eclipse main window

What next?
Now you should have everything setup in order to develop and android applications. I would recommend the google tutorials: