Wednesday, April 27, 2011

Using Twitter4J to Tweet in Android

So I'm working on a little project for Google I/O that requires, amongst other things, the ability to post status updates to Twitter from within an Android app. I asked about it on Twitter and a couple of people asked me to post the results (and associated code snippets) so here you go.

I was hoping for a small code snippet that would let me do that without needing any third-party libraries, but the feedback from the lazy web suggested that jumping through the hoops of an OAuth implementation myself wasn't worth the effort.

The wisdom of crowds suggested Twitter4J as a simple alternative - and as the following code snippet shows - the most simple case is pleasantly simple to implement.

Twitter twitter = new TwitterFactory().getInstance();
AccessToken a = new AccessToken(oauth_token, oauth_token_secret);
twitter.setOAuthConsumer(consumer_token, consumer_secret);
twitter.setOAuthAccessToken(a);
twitter.updateStatus("If you're reading this on Twitter, it worked!");


In this instance I'm the only one who'll be using the app, so I'm dropping an auth token and auth token secret unique to my own Twitter login rather than going through the process required to obtain a user-specific auth token. If that matches your use-case you can grab those values by clicking "My Access Token" on the Twitter developer site after you've registered your app.

You can download Twitter4J for Android here. Then just add twitter4j-core-android-2.2.1.jar into your project as an external JAR.

Tuesday, April 12, 2011

I'm Saying Goodbye to London

Where to next?

Mountain View in sunny California!

Following this year's Google I/O, I'll be relocating to the home of the giant dessert sculpture garden, where I've been given the opportunity to take on the role of Tech Lead for the global Android Developer Relations team.

It's a chance for me to focus on some more strategic ideas and to work more closely with the core Android engineering team. It's a challenge I'm really looking forward to.

I've spent the last 6 years in London - the last 2 working here at Google - and it's been an amazing experience. I'll be leaving the Android developers of EMEA in the very capable hands of Nick ButcherRichard Hyndman, and Robert Rhode - and I'll still visit, I'm particularly looking forward to this year's round of Google Developer Days.

I'm still working at Google, and I'm still part of the Android team, so being based in Mountain View I'll have the opportunity to meet and work with some of our North American Android devs - so be sure to say hi if you're coming to Google I/O this year.

The move is still some months away, but in the mean time here are some of the things I will (and won't) miss about London, and what I'm looking forward to in California.

Things I'll Miss About London
  • World class theatre, restaurants, and concerts all at my door step.
  • Living in close proximity to the rest of Europe.
  • Proper bacon and real cheddar cheese.
  • Seasons (particularly Spring and Autumn).
  • The awesome Android developers I've worked with over the last 2 years.
  • Full English breakfasts.
Things I Won't Miss About London
  • Commuting for an hour every morning, and again every evening.
  • The Victoria line and South West Trains.
  • Hearing my neighbor snoring.
  • Driving in London.
  • Black pudding.
Things I'm Looking Forward to in California
  • Fruit that tastes like fruit.
  • Living on the West coast (the best coast).
  • Living in close proximity to an ocean.
  • Living in close proximity to the rest of the US.
  • Wide roads and cheap(er) petrol gas. 
  • American breakfasts.