extend

My Android AsyncTask docs and examples (parameters, callbacks, executing, canceling)

I’ve currently written this document as a “note to self” about how the Android AsyncTask works. It’s currently incomplete, but if you want to know how an AsyncTask works, most of the answers are generally here. I provide documentation for most aspects of the AsyncTask, though my coverage of (a) updating progress/status and (b) canceling an AsyncTask is a little weak atm.

How to extend a Java interface like a Scala trait

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.9, “How to extend a Java interface like a Scala trait.”

Problem

You want to implement a Java interface in a Scala application.

Solution

In your Scala application, use the extends and with keywords to implement your Java interfaces, just as though they were Scala traits.

Given these three Java interfaces:

How to use Scala traits as simple mixins

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 8.4, “How to use Scala traits as simple mixins (or, How to mix in Scala traits).”

Problem

You want to design a solution where multiple traits can be mixed into a class to provide a robust design.

Solution

To implement a simple mixin, define the methods you want in your trait, then add the trait to your class using extends or with. For instance, the following code defines a Tail trait:

How to declare constructor parameters when extending a Scala class

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.10, “How to handle constructor parameters when extending a Scala class.”

Problem

You want to extend a base Scala class, and need to work with the constructor parameters declared in the base class, as well as new parameters in the subclass.

Scala: How to add new methods to existing classes

Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.

A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.

A simple Android ListActivity example

Android ListActivity FAQ: How do I create a class that extends the Android ListActivity class?

The easiest way to show a ListActivity example is to share some source code with the non-essential code stripped out. To that end, here's an example ListActivity class:

Improve your MacBook battery life (2011 edition)

MacBook battery life FAQ: How can I improve my MacBook battery life (or MacBook Pro or MacBook Air battery life)?

I've written about MacBook battery life before (see the links below), but with a new MacBook Air, I've become increasingly interested in MacBook battery life. I just did some more battery life research on the Apple website, and here's what I found.

How to extend MacBook Pro battery life, part 2

As a quick follow up to my previous article on how to extend MacBook battery life, I just tested my MacBook Pro battery life today, and I got almost 2.5 hours out of today's battery charge(!).

Following my own MacBook battery advice, when I sat down to work I shut down Firefox and turned off the wireless networking. After that I went to work as normal, typing into a text editor for 2.5 hours straight.