Tuesday, 27 December 2011

Android: Twitter login using twitter4j library

At the time of writing the best tutorial I could find for integrating Twitter into an Android app using the twitter4j library was this one...


... I set up Twitter user authentication as in the tutorial above such that the Activity doing the Twitter authentication has its content view changed to a WebView, the Activity is declared in the manifest file containing an intent-filter to catch the Twitter callback and, on callback, the response is processed and the Activity's content view is changed back to whatever it was.

I got it working but in doing so I found (what I think is) an easier way and such that the WebView doesn't ever kick off the phone's web browser app (thereby leaving your app, which you don't really want), as follows:

(1) Remove the Activity singleInstance and intent-filter declarations you added to the manifest file (in following the tutorial above). Don't need that anymore!

(2) Add a WebView to your Activity layout file and set its initial visibility to "gone" and its position, height, width etc however you want it. I set its properties so that it covers the whole screen when visible, as follows:

  android:visibility="gone" />

(3) Get a handle to your WebView in your Activity and set its WebViewClient to pick up the Twitter callback (and not to kick off the web browser app at any time), as follows...

myWebView = (WebView)findViewById(R.id.myWebView);
myWebView.setWebViewClient(new WebViewClient()
  public boolean shouldOverrideUrlLoading(WebView webView, String url)
    if (url != null
        && url.startsWith("myapptwittercallback:///myapp"))
    return true;

... Here you might also want to enable other settings of your WebView like saving form data and javascript execution (e.g. myWebView.getSettings().setJavaScriptEnabled(true);)

(4) Make your WebView visible and focused at the point you need to do Twitter authentication, as follows:

  twitter = new TwitterFactory().getInstance();
  twitterRequestToken = twitter.getOAuthRequestToken(
catch (TwitterException ex)
      "Login failed. Please try again.",

... Remember to clear your WebView's history (i.e. myWebView.clearHistory() and set its visibility to "gone" in your handleTwitterCallback(String) method.

(5) Lastly, override your Activity's onBackPressed() method so that your WebView handles presses of the back button if its visible, as follows...

public void onBackPressed()
  if (myWebView.getVisibility() == View.VISIBLE)
    if (myWebView.canGoBack())

That's it! You should now have Twitter integration in your app and the user should never leave your app in doing Twitter authentication.

Saturday, 17 December 2011

Testing your Map and Location-aware app on the Android emulator

First of all, start up an emulator and get your app running on the emulator (from within Eclipse or however you do it). Sometimes the Android Debug Bridge (or maybe it's the Android Virtual Device manager that) gets its pants in a twist and it can't find the emulator you've started up. In this case you should run the following sequence of commands from the command prompt: 'adb kill-server'; 'adb start-server'; 'adb devices'; and then 'adb devices' again to be doubly sure you can see your emulator in the list of devices!

Next, with your app running on the emulator, start up the Dalvik Debug Monitor (by double-clicking the 'ddms' Windows batch file in the android-sdk 'tools' folder) and select your emulator (from the list of running emulators) as the one to feed data into. Select the 'Emulator Control' tab (still within the Dalvik Debug Monitor), scroll down to 'Location Controls', and with the 'Decimal' radio button selected (rather than the 'Sexagesimal' radio button), enter the Latitude and Longitude values to feed to your app (running on the emulator).

For London: enter Latitude as 51.5 and Longitude as -0.13.
For Belfast: enter Latitude as 54.6 and Longitude as -5.93.
And so on and so forth for various Latitude (north/south) and Longitude (east/west) values which you can find on websites like getlatlon.com.