[This post is by Ian Ni-Lewis, a Developer Advocate who devotes most of his time to making Android games more awesome. — Tim Bray]
Making a game on Android is easy. Making a great game for a mobile, multitasking, often multi-core, multi-purpose system like Android is trickier. Even the best developers frequently make mistakes in the way they interact with the Android system and with other applications*ó*mistakes that donít affect the quality of gameplay, but which affect the quality of the userís experience in other ways.
A truly great Android game knows how to play nice: how to fit seamlessly into the system of apps, services, and UI features that run on Android devices. In this multi-part series of posts, Android Developer Relations engineers who specialize in games explain what it takes to make your game play nice.
I: The Audio Lifecycle (or, why is there music coming from my pants?)

One of the most awesome things about Android is that it can do so much stuff in the background. But when apps arenít careful about their background behaviors, it can get annoying. Take my own personal pet peeve: game audio that doesnít know when to quit.
The problem

Iím on the bus to work, passing the time with a great Android game. Iím completely entranced by whatever combination of birds, ropes, and ninjas is popular this week. Suddenly I panic: Iíve almost missed my stop! I leap up, quickly locking my phone as I shove it into a pocket.
I arrive breathless at my first meeting of the day. The boss, perhaps sensing my vulnerability, asks me a tough question. Not tough enough to stump me, though*ó*Iíve got the answer to that right here on my Android phone! I whip out my phone and press the unlock button... and the room dissolves in laughter as a certain well-known game ditty blares out from the device.
The initial embarrassment is bad enough, but whatís this? I canít even mute the thing! The phone is showing the lock screen and the volume buttons are inactive. My stress level is climbing and it takes me three tries to successfully type in my unlock code. Finally I get the thing unlocked, jam my finger on the home button and breathe a sigh of relief as the music stops. But the damage is done*ó*my boss is glowering and for the rest of the week my co-workers make video game noises whenever they pass my desk.
What went wrong?

Itís a common mistake: the developer of the game assumed that if the game received an onResume() message, it was safe to resume audio. The problem is that onResume() doesnít necessarily mean your app is visible*ó*only that itís active. In the case of a locked phone, onResume() is sent as soon as the screen turns on, even though the phoneís display is on the lock screen and the volume buttons arenít enabled.
Fixing this is trickier than it sounds. Some games wait for onWindowFocusChanged() instead of onResume(), which works pretty well on Gingerbread. But on Honeycomb and higher, onWindowFocusChanged() is sent when certain foreground windows*ó*like, ironically, the volume control display window*ó*take focus. The result is that when the user changes the volume, all of the sound is muted. Not the developerís original intent!
Waiting for onResume() and onFocusChanged() seems like a possible fix, and it works pretty well in a large number of cases. But even this approach has its Achillesí heel. If the device falls asleep on its own, or if the user locks the phone and then immediately unlocks it, your app may not receive any focus changed messages at all.
What to do about it

Hereís the easy two-step way to avoid user embarrassment:
  1. Pause the game (and all sound effects) whenever you receive an onPause() message. When gameplay is interrupted*ó*whether because the phone is locked, or the user received a call, or for some other reason*ó*the game should be paused.
  2. After the game is paused, require user input to continue. The biggest mistake most game developers make is to automatically restart gameplay and audio as soon as the user returns to the game. This isnít just a question of solving the ďmusic over lock screenĒ issue. Users like to come back to a paused game. Itís no fun to switch back to a game, only to realize youíre about to die because gameplay has resumed before you expected it.
Some game designers donít like the idea of pausing the background music when the game is paused. If you absolutely must resume music as soon as your game regains focus, then you should do the following:
  1. Pause playback when you receive onPause().
  2. When you receive onResume():
    1. If you have previously received an onFocusChanged(false) message, wait for an onFocusChanged(true) message to arrive before resuming playback.
    2. If you have not previously received an onFocusChanged(false) message, then resume audio immediately.
  3. Test thoroughly!
Fixing audio embarrassments is almost always a quick and easy process. Take the time to do it right, and your users will thank you.